0

I've read a lot of questions with this issue, but I've noticed that it happens for many many reasons, and haven't found an answer to my particular problem. The thing is, I've accidentally deleted some files and used photorec to try and recover them (I'm on Ubuntu 16.04 Xenial). The problem is, it recovered 2600+ folders! I'm writing a little filecrawler to get rid of the files I don't need - the only files that interest me are .py,.jpg,.png and .mp3. Here is the code to my crawler:

import os

path=

for (path, dirs, files) in os.walk(path):
    for name in files:
        if name.endswith('.py') or name.endswith('.jpg') or name.endswith('.png') or name.endswith('.mp3'):
            continue
        else: os.remove(name)

I define 'path' manually, and I've done a test where I print part of the paths,dirs and files, and it prints what I want. However, when running the code below, it returns:

OSError: [Errno 2] No such file or directory: 'f0272560.java'

Which I'm assuming is the first file I try to remove. I'm guessing that it is looking for the file again, after it was deleted already. Does this have sense or it is anything else raising the error? How could I fix this?

Thanks in advance!

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
DarthDante
  • 3
  • 1
  • 4

1 Answers1

3

The files list yielded by os.walk is the unqualified names of the files. Unless they happen to be in the working directory, os.remove can't remove them by unqualified names, because it doesn't know where to find them. Change:

os.remove(name)

to

os.remove(os.path.join(path, name))

so you pass a qualified path to the file, not just its name.

Side-note: While it works out fine if you never need the original definition of path, overwriting it with the per-loop path from os.walk may be confusing. You might want to change the name of one of them, either changing the top level one to rootdir or changing the loop variable name to something else (e.g. curdir).

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Thanks a lot! It was unclear for me how to correctly use os.walk – DarthDante Sep 23 '16 at 16:53
  • @DarthDante: Yar, `os.listdir` is confusing for the same reasons. For `os.listdir`, I recommend replacing it with `os.scandir` (3.5 or higher only sadly; pre-3.5, you need the PyPI package `scandir` to use the equivalent `scandir.scandir`) where the entries returned have a `.name` attribute for the bare name, and `.path` for the qualified path; for `os.walk`, it's not a terrible idea to have the first two lines inside the `walk` loop be `qdirs = [os.path.join(path, d) for d in dirs]`, `qfiles = [os.path.join(path, f) for f in files]` so you can iterate `qdirs`/`qfiles` for pre-qualified paths. – ShadowRanger Sep 23 '16 at 18:12