wait for shutil.copyfile to finish
Asked Answered
L

1

7

I want to copy a file then start writing the new file:

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

However, when I do the above hello will be written before the end of the file. What is the right way to make sure copyfile is done?

I looked on SO and other places but all the answers I saw said that shutil.copyfile blocks or locks and that it shouldn't be a problem. And yet, it is. Please help!

Loosen answered 7/2, 2013 at 0:52 Comment(1)
Suspicious. Can you provide a self-contained example showing the problem?Kolnos
K
3

Try using copyfileobj directly instead:

with open('largefile', 'r') as f1, open('newlargefile', 'w') as f2:
    shutil.copyfileobj(f1, f2)
    f2.write('hello')
Kolnos answered 7/2, 2013 at 0:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.