无法使用操作系统或 shutil 模块删除 zip 文件



我试图将整个目录的内容压缩到一个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:UsersaryanAppDataLocalProgramsPythonPython36-32libsite-packagessend2trashplat_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:UsersaryanAppDataLocalProgramsPythonPython36-32libshutil.py", line 494, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:UsersaryanAppDataLocalProgramsPythonPython36-32libshutil.py", line 376, in _rmtree_unsafe
    onerror(os.listdir, path, sys.exc_info())
  File "C:UsersaryanAppDataLocalProgramsPythonPython36-32libshutil.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'

要通过python删除zip文件夹,您需要使用os.remove(myzip.zip)因为它被视为文件而不是文件夹,因此shutil.rmtree()不起作用。

来源:我以艰难的方式学到了这一点

我创建了一个像你这样名为"new.zip"的zip文件,并通过以下方式成功删除了它: os.remove('new.zip')

最新更新