How do you make a copy of a file with Python?

Questions & AnswersCategory: PythonHow do you make a copy of a file with Python?
Lokesh Kumar Staff asked 2 years ago

How do you make a copy of a file with Python?
a. copyfile(‘file.txt’, ‘file_copy.txt’)
 
b. import shutil
shutil.copyfile(‘file.txt’, ‘file_copy.txt’)
 
c. open(‘file.txt’, ‘file_copy.txt’)
 
d. copy(‘file.txt’, ‘file_copy.txt’)

1 Answers
Lokesh Kumar Staff answered 2 years ago

b. import shutil
shutil.copyfile(‘file.txt’, ‘file_copy.txt’)
Explanation:

shutil.copyfileobj(fsrcfdst[, length]) – copy the contents of the file-like object fsrc to the file-like object fdst.