-3

There is a repository with folders and images inside these folders. The point is that I cant figure out how this method os.remove works. In some folders it removes unneeded files , in some doesn't, also some folders is shrinking in sizes ( 10-15 images ), some isn't. What am I missing here?

dirs = next(os.walk(path))[1]

for d in dirs:
    dirPath = path  + d
    os.chdir(dirPath)

    dirPath = path  + d


    files = next(os.walk(dirPath))[2]

    for f in files:
        if f is 'feature.bin': os.remove('feature.bin')      
        if f is 'filelist_LBP.txt': os.remove('filelist_LBP.txt')
        if f is 'info.txt': os.remove('info.txt')  
Ivan Shelonik
  • 1,958
  • 5
  • 25
  • 49
  • Have you checked that the dirs variable is being set to something? Because if it's the child directory (i.e. with no directories below it) you won't walk through any files – TLOwater Jun 06 '17 at 13:33
  • Also, avoid comparisons using "is" when you mean "==". See https://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce for more details. – xorsyst Jun 06 '17 at 13:38

1 Answers1

4

os.remove needs the full path to the file to be deleted, unless it happens to be in the current directory.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101