等待shutil.copyfile完成

4my*_*yle 5 python shutil file-copying

我想复制一个文件,然后开始编写新文件:

shutil.copyfile("largefile","newlargefile")
nwLrgFile=open("newlargefile",'a')
nwLrgFile.write("hello\n")
Run Code Online (Sandbox Code Playgroud)

但是,当我这样做时,hello将在文件结束之前写入.确保copyfile完成的正确方法是什么?

我查看了SO和其他地方但我看到的所有答案都说shutil.copyfile阻塞或锁定它应该不是问题.然而,确实如此.请帮忙!

nne*_*neo 3

尝试copyfileobj直接使用:

with open('largefile', 'r') as f1, open('newlargefile', 'w') as f2:
    shutil.copyfileobj(f1, f2)
    f2.write('hello')
Run Code Online (Sandbox Code Playgroud)