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? Thank you in advance.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
Z77
  • 1,097
  • 6
  • 19
  • 30

1 Answers1

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))
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • @TimPietzcker: True; it will remove anything without `zipped.zip` anywhere in its filename. – unutbu May 14 '12 at 09:38
  • Ah, I thought your solution was supposed to replace the entire code snippet by Z77, not just the `os.remove()` line. My bad. – Tim Pietzcker May 14 '12 at 09:40
  • Now I got another error: Error Info: [Error 5] Access is denied: 'C:/path\\geogeo.gdb' – Z77 May 14 '12 at 09:59
  • 1
    There is a problem because gdb is considered is subfolder? Because I can delete all other files in main folder, just not gdb. – Z77 May 14 '12 at 11:23
  • 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 :((( – Z77 May 14 '12 at 12:30
  • Solution to my problem is http://stackoverflow.com/a/6615332/334172 Latest post! – Z77 May 14 '12 at 12:45