Z77
Z77

Reputation: 1165

Deleting files in folder with python

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?

Upvotes: 2

Views: 3579

Answers (1)

unutbu
unutbu

Reputation: 880717

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))

Upvotes: 7

Related Questions