![](/img/trans.png)
[英]shutil.move -> WindowsError: [Error32] The process cannot access the file
[英]WindowsError: [Error 32] when I do shutil.move?
我收到WindowsError: [Error 32] The process cannot access the file because it is being used by another process
除外塊中的WindowsError: [Error 32] The process cannot access the file because it is being used by another process
。 怎么會這樣
try:
shutil.move(os.path.join(folder, name), folder)
except shutil.Error:
# Handling Error: Destination already exists
unique_name = "%s_%s" %(name, time.time())
shutil.move(os.path.join(folder, name), os.path.join(folder, unique_name))
假設我可以在Windows中處理這樣的異常(我希望第一個shutil.move
不會鎖定文件或存在異常的任何東西),就像shutil.Error
塊中的try塊中引發了錯誤,因為文件已經存在存在,它進入了except塊,試圖用新文件名移動它,只是發現它被另一個進程使用了?
如果我要以Python線程的形式編寫shutil.move
操作,那么是否有可能兩個線程試圖做同樣的事情? 還是不會發生這種情況,因為有一個GIL鎖強制順序執行?
在Windows中,一般來說,我可以同時打開兩個文件進行寫入嗎?
還是因為按順序進行GIL鎖定而不會發生?
即使線程是按順序發生的,這也不意味着它們會釋放其文件處理程序,這意味着當使用該線程的線程處於非活動狀態時,該文件仍處於鎖定狀態。
就像shutil.try塊中發生了錯誤,因為該文件已經存在
如果您的shutil.move
失敗,則意味着問題在於讀取源文件,而不是寫入目標文件。 還有另一個進程(或者可能是您自己的Python進程中的更早的進程?)打開了文件,並且沒有取消對文件的讀取鎖定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.