Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Answer in python 3.6) Write a function file_copy() that takes two string parame

ID: 3600092 • Letter: #

Question

(Answer in python 3.6)
Write a function file_copy() that takes two string parameters (in_file and out_file) and copies the content of in_file into out_file. Assume that in_file exists before file_copy is called. For example, the following would be correct input and output.

>>> file_copy('created_equal.txt', 'copy.txt')
>>> open_f = open('copy.txt')
>>> equal_f = open('created_equal.txt')
>>> equal_f.read() 'We hold these truths to be self-evident, that all men are created equal '

Explanation / Answer

the requested code for python 3.6 is as follows :

----------------------------------------------------------------------------------------

#!usr/bin/env python

def file_copy(infile, outfile):
    in_f = open(infile,'r')             # 'r' mention read mode
    out_f = open(outfile,'w')       # 'w' mention write mode
    for line in in_f:
        out_f.write(line)
    out_f.close()
    in_f.close();           # closing the files

-----------------------------------------------------------------------------------------------

you have to call the above method to copy the infile to outfile

In this function the infile is copied/ to outfile line by line in the for loop.

There are other methods for file objects like read() - which read the whole data at a time

readline() - read a line once per call.

etc.,

so the above code can also be rewritten as

---------------------------------------------------------------------------------------

#!usr/bin/env python

def file_copy(infile, outfile):
    in_f = open(infile,'r')             # 'r' mention read mode
    out_f = open(outfile,'w')       # 'w' mention write mode
    out_f.write(in_f.read())
    out_f.close()
    in_f.close();


-----------------------------------------------------------------------------------------------------------------------

both are one and same.

Take care that the opened files are closed properly

/* hope this helps */

/* if any queries, please comment */

/* thank you */