I have one folder with one geodatabase and two other files (txt). I used zip and zipped them. So now in this folder i have gdb, txt,txt, and new zip file. Now I need to delete those files that were zipped, so that will be only zip file in the folder. I wrote the following code:
def remove_files():
for l in os.listdir(DestPath):
if l.find('zipped.zip') > -1:
pass
else:
print ('Deleting ' + l)
os.remove(l)
But got:
Error Info:
[Error 2] The system cannot find the file specified: 'geogeo.gdb'
Can anyone help me?
ggorlen
59.1k8 gold badges118 silver badges173 bronze badges
asked May 14, 2012 at 9:33
1 Answer 1
os.listdir
only returns filenames, not complete paths.
os.remove
uses the current working directory if only a filename is given. If the current working directory is different than DestPath
, then you need to supply the full path:
os.remove(os.path.join(DestPath,l))
answered May 14, 2012 at 9:35
6 Comments
unutbu
@TimPietzcker: True; it will remove anything without
zipped.zip
anywhere in its filename.Tim Pietzcker
Ah, I thought your solution was supposed to replace the entire code snippet by Z77, not just the
os.remove()
line. My bad.Z77
Now I got another error: Error Info: [Error 5] Access is denied: 'C:/path\\geogeo.gdb'
Z77
There is a problem because gdb is considered is subfolder? Because I can delete all other files in main folder, just not gdb.
Z77
def remove_files(): for l in os.listdir(DestPath): if 'zipped.zip' in l: pass elif '.gdb.' in l: rmtree(os.path.join(DestPath,l)) else: print ('Deleting ' + l) os.remove(os.path.join(DestPath,l)) I tried this delete also file.gdb as a folder..But still have problem with [error 5] Acesss is denied. I really do not know what to do :(((
|
lang-py
l.find('zipped.zip') > -1
you can simply use'zipped.zip' in l