Wait for shutil.copyfile to complete

I want to copy a file and start writing a new file:

shutil.copyfile("largefile","newlargefile")
nwLrgFile=open("newlargefile",'a')
nwLrgFile.write("hello\n")

However, when I do the above, it hellowill be written to the end of the file. What is the correct way to make a copy of a file?

I looked at SO and other places, but all the answers I saw said that shutil.copyfile is blocking or blocking and that this should not be a problem. And yet, this is so. Please, help!

+5
source share
1 answer

Try using instead copyfileobj:

with open('largefile', 'r') as f1, open('newlargefile', 'w') as f2:
    shutil.copyfileobj(f1, f2)
    f2.write('hello')
+2
source

All Articles