2

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
  • 3
    Instead of that l.find('zipped.zip') > -1 you can simply use 'zipped.zip' in l Commented May 14, 2012 at 9:36

1 Answer 1

7

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

@TimPietzcker: True; it will remove anything without zipped.zip anywhere in its filename.
Ah, I thought your solution was supposed to replace the entire code snippet by Z77, not just the os.remove() line. My bad.
Now I got another error: Error Info: [Error 5] Access is denied: 'C:/path\\geogeo.gdb'
There is a problem because gdb is considered is subfolder? Because I can delete all other files in main folder, just not gdb.
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 :(((
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.