我试图将整个目录的内容压缩成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 :(得分:3)
要通过python
删除zip文件夹,您需要使用os.remove(myzip.zip)
,因为它被视为文件而不是文件夹,因此shutil.rmtree()
无效。
资料来源:我很难学到这一点
答案 1 :(得分:0)
我创建了一个名为&#39; new.zip&#39;的zipfile,并成功通过以下方式将其删除:
os.remove('new.zip')