當我嘗試解壓縮檔案并洗掉舊檔案時??,它說它仍在運行,所以我使用了關閉功能,但它沒有關閉它。
這是我的代碼:
import zipfile
import os
onlineLatest = "testFile"
myzip = zipfile.ZipFile(f'{onlineLatest}.zip', 'r')
myzip.extractall(f'{onlineLatest}')
myzip.close()
os.remove(f"{onlineLatest}.zip")
我收到這個錯誤:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'Version 0.1.2.zip'
有人知道怎么修這個東西嗎?
只有之前運行它的其他部分,但不要認為它的問題:
request = service.files().get_media(fileId=onlineVersionID)
fh = io.FileIO(f'{onlineLatest}.zip', mode='wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
myzip = zipfile.ZipFile(f'{onlineLatest}.zip', 'r')
myzip.extractall(f'{onlineLatest}')
myzip.close()
os.remove(f"{onlineLatest}.zip")
uj5u.com熱心網友回復:
嘗試使用。這樣,您根本不必關閉。:)
with ZipFile(f'{onlineLatest}.zip', 'r') as zf:
zf.extractall(f'{onlineLatest}')
uj5u.com熱心網友回復:
將評論中的討論總結為答案:
在 Windows 作業系統上,與 Linux 不同,如果系統上有任何行程在該檔案上打開了檔案句柄,則無法洗掉該檔案。
在這種情況下,您通過句柄寫入檔案fh并通過myzip. 在洗掉它之前,您必須關閉兩個檔案句柄。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/367711.html
