2013-02-07 229 views
5

我要复制一个文件,然后开始写新的文件:等待shutil.copyfile完成

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

然而,当我做上述hello将文件结束前被写入。什么是确保复制文件完成的正确方法?

我看着SO和其他地方,但所有的答案,我看到说shutil.copyfile块或锁,它不应该是一个问题。然而,它是。请帮忙!

+1

可疑。你能提供一个独立的例子来显示问题吗? – nneonneo

回答

2

尝试使用copyfileobj,而不是直接:

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