Computer Science, asked by kritikamehta122, 9 months ago

Prepare a program in python that copies a text file "source.text" onto "target.text" barring the lines starting with character 'U'.​

Answers

Answered by Oreki
4

from os import path

if path.isfile("source.txt") and path.isfile("target.txt"):

with open("source.txt", 'r') as source:

with open("target.txt", 'w') as target:

for sentence in source.readlines():

if not sentence.startswith('U'):

target.write(sentence)

print("Task executed successfully!")

else: print("Some files seem to be missing...")

Answered by nadhoose
12

Answer:

def copy():

     f1=open("source.txt","r")

     f2=open("target.txt","w")

     while True:

           data=f1.readline()

           if len(data)==0:

                 break

           elif data[0]=="U":

                 continue

           f2.write(data)

     print("file copied successfully")

     f1.close()

     f2.close()

copy()

Similar questions