2013-02-07 226 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')