等待shutil.copyfile完成
我想复制一个文件,然后开始写入新的文件:
shutil.copyfile("largefile","newlargefile")
nwLrgFile=open("newlargefile",'a')
nwLrgFile.write("hello\n")
但是,当我这样做的时候,hello
会在文件还没复制完的时候就被写入。有什么正确的方法可以确保复制文件的操作完成了吗?
我在StackOverflow和其他地方查过,看到的答案都说shutil.copyfile是会阻塞或锁定的,应该不会有问题。但实际上,问题还是存在。请帮帮我!
1 个回答
3
试着直接使用 copyfileobj
吧:
with open('largefile', 'r') as f1, open('newlargefile', 'w') as f2:
shutil.copyfileobj(f1, f2)
f2.write('hello')