Reputation: 191
i was trying to zip the contents of the entire dir into a zip file. Later when i am trying to delete the same its failing. Am i missing something here?
>> 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'
Upvotes: 1
Views: 3569
Reputation: 1015
I created a zipfile like you named 'new.zip', and successed to delete it via:
os.remove('new.zip')
Upvotes: 0
Reputation: 3635
To delete a zip folder via python
you need to use os.remove(myzip.zip)
because it is treated as a file not a folder so shutil.rmtree()
doesnt work.
Source: i learnt this the hard way
Upvotes: 5