2017-05-22 61 views
0

我試圖將整個目錄的內容壓縮成zip文件。後來當我試圖刪除相同的失敗。我在這裏錯過了什麼嗎?無法使用os或shutil模塊刪除zip文件

>> import zipfile 

>>> import os 
>>> os.listdir() 
['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 
'python.exe', 'python3.dll', 'python36.dll', 'pythonw.exe', 'Scripts', 
'tcl', 'Tools', 'vcruntime140.dll'] 
>>> os.chdir('C:\\Users\\aryan') 
>>> os.listdir() 
['.idlerc', '.PyCharm2017.1', 'AppData', 'Application Data', 'Contacts', 
'Cookies', 'Desktop', 'Documents', 'Downloads', 'Favorites', 'LEH2016', 
'Links', 'Local Settings', 'Music', 'My Documents', 'NetHood', 'NTUSER.DAT', 
'ntuser.dat.LOG1', 'ntuser.dat.LOG2', 'NTUSER.DAT{ac08763f-3218-11e7-8556- 
93c7ff812bb9}.TM.blf', 'NTUSER.DAT{ac08763f-3218-11e7-8556- 
93c7ff812bb9}.TMContainer00000000000000000001.regtrans-ms', 
'NTUSER.DAT{ac08763f-3218-11e7-8556- 
93c7ff812bb9}.TMContainer00000000000000000002.regtrans-ms', 'ntuser.ini', 
'OneDrive', 'os.walk_result.txt', 'Pictures', 'PrintHood', 
'PycharmProjects', 'Recent', 'Saved Games', 'Searches', 'SendTo', 'Start 
    Menu', 'Templates', 'Videos'] 

>>> zip=zipfile.ZipFile('new.zip','w') 
>>> zip.write('C:\\Users\\aryan',compress_type=zipfile.ZIP_DEFLATED) 
>>>os.listdir('C:\\Users\\aryan') 
['.idlerc', '.PyCharm2017.1', 'AppData', 'Application Data', 'Contacts', 'Cookies', 'Desktop', 'Documents', 'Downloads', 'Favorites', 'leh.zip', 'LEH2016', 'Links', 'Local Settings', 'Music', 'My Documents', 'NetHood', 'new.zip', 'NTUSER.DAT', 'ntuser.dat.LOG1', 'ntuser.dat.LOG2', 'NTUSER.DAT{ac08763f-3218-11e7-8556-93c7ff812bb9}.TM.blf', 'NTUSER.DAT{ac08763f-3218-11e7-8556-93c7ff812bb9}.TMContainer00000000000000000001.regtrans-ms', 'NTUSER.DAT{ac08763f-3218-11e7-8556-93c7ff812bb9}.TMContainer00000000000000000002.regtrans-ms', 'ntuser.ini', 'OneDrive', 'os.walk_result.txt', 'Pictures', 'PrintHood', 'PycharmProjects', 'Recent', 'Saved Games', 'Searches', 'SendTo', 'Start Menu', 'Templates', 'Videos'] 
>>> 
>>> import send2trash 
>>> send2trash.send2trash('C:\\Users\\aryan\\new.zip') 
Traceback (most recent call last): 
    File "<pyshell#22>", line 1, in <module> 
    send2trash.send2trash('C:\\Users\\aryan\\new.zip') 
    File "C:\Users\aryan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\send2trash\plat_win.py", line 58, in send2trash 
    raise OSError(msg) 
OSError: Couldn't perform operation. Error code: 32 
>>> os.unlink('C:\\Users\\aryan\\new.zip') 
Traceback (most recent call last): 
    File "<pyshell#25>", line 1, in <module> 
    os.unlink('C:\\Users\\aryan\\new.zip') 
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\aryan\\new.zip' 
>>> 

>>> os.path.exists('C:\\Users\\aryan\\new.zip') 
True 

>>> import shutil 

>>> shutil.rmtree('C:\\Users\\aryan\\new.zip') 
Traceback (most recent call last): 
    File "<pyshell#40>", line 1, in <module> 
    shutil.rmtree('C:\\Users\\aryan\\new.zip') 
    File "C:\Users\aryan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 494, in rmtree 
    return _rmtree_unsafe(path, onerror) 
    File "C:\Users\aryan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 376, in _rmtree_unsafe 
    onerror(os.listdir, path, sys.exc_info()) 
    File "C:\Users\aryan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 374, in _rmtree_unsafe 
    names = os.listdir(path) 
NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\Users\\aryan\\new.zip' 

>>> zip.close() 

>>> os.unlink('C:\\Users\\aryan\\leh.zip') 
Traceback (most recent call last): 
    File "<pyshell#70>", line 1, in <module> 
    os.unlink('C:\\Users\\aryan\\leh.zip') 
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\aryan\\leh.zip' 
+0

從shutil文檔:「刪除整個目錄樹;路徑必須指向一個目錄「。你指向一個文件。另外我認爲你永遠不會關閉文件。嘗試使用帶有上下文管理器的ZipFile,即「with ZipFile ..」,在ZipFile下的Python文檔中有一個示例。 'zip.close()'之前的 – jockster

+0

這很正常,您不能刪除該文件或將其移至垃圾箱。之後,它不是。請[編輯]您的問題,以顯示[mcve] –

回答

2

要通過python刪除Zip文件夾,您需要使用os.remove(myzip.zip),因爲它是作爲一個文件不是一個文件夾,以便shutil.rmtree()行不通的對待。

來源:我瞭解到這個硬盤的方式

+1

感謝它的工作。 – Ary

0

我創建了一個zip文件就像你被點名了「new.zip」,並通過successed刪除它: os.remove('new.zip')