Create a function copy() that reads one file line by line and copies each line to another file. You should use try-except statements and use either open and close file statements or a with file statement.>>>def copy(filename1, filename2) :

Respuesta :

Answer:

See attachment for answer

Explanation:

I could not add the answer; So, I made use of an attachment

The program is written on 9 lines and the line by line explanation is as follows:

Line 1:

This defines the function

Line 2:

This begins a try except exception that returns an exception if an error is encountered

Line 3:

This open the source file using open and with statement

Line 4:

This open the destination file using open and with statement and also makes it writable

Line 5:

This iterates through the content of the source file; line by line

Line 6:

This writes the content to the destination file

Line 7 & 8:

This is returned if an error is encountered

Line 9:

This prints that the file has been successfully copied        

To call the function from main; use

copy(filename1, filename2)

Where filename1 and filename2 are names of source and destination files.

Take for instance:

filename1 = "test1.txt"

filename1 = "test2.txt"

copy(filename1, filename2)

The above will copy from test1.txt to test2.txt

Ver imagen MrRoyal
fichoh

The function copies text from one file to another. The program written in python 3 goes thus

def copy(filename1, filename2):

#initialize a function names copy and takes two arguments which are two files

try:

#initiate exception to prevent program throwing an error

with open(filename1, "r") as file1 :

#open file1 in the read mode with an alias

with open(filename2, "w") as file2:

#open file 2 in write mode with an alias

for line in file1:

#loop through each line in file 1

file2.write(line)

#copy / write the line in file2

except:

print("Error copying file")

#display if an error is encountered

print("File Copied")

#if no error, display file copied

Learn more : https://brainly.com/question/22392664