Suppose I have a directory tree like this:
a/
├── b
│ ├── d.png
│ └── e
│ └── f.png
└── c.png
My desired output is:
if I want to search for files in a/b/
:
b/d.png
& b/e/f.png
and in case of a/
: a/c.png
a/b/d.png
a/b/e/f.png
I know how to list all the files in a subdirectory using os.walk and I searched a lot for similar situation like mine but I didn't find any.
Here's my latest attempt:
path = "a/b"
pathBase = os.path.basename(path)
for p, dirs, files in os.walk(path):
for file in files:
print(os.path.join(p.replace(path, pathBase),file))
while this works as expected with a/b/
it doesn't work with a/
.
Output for a
is: c.png
b/d.png
b/e/f.png
which is not as expected.
Your code will be (almost) correct if you strip the trailing slash from path
, but in any case you should use os.path.relpath
to compute the relative file paths:
import os
def f(path):
path = path.rstrip(os.sep)
parent = os.path.dirname(path)
for p, dirs, files in os.walk(path):
for file in files:
print(os.path.relpath(os.path.join(p, file), parent))
For what it's worth, pathlib
makes path manipulation easier:
from pathlib import Path
def f(path):
path = Path(path)
for x in path.rglob('*'):
if not x.is_dir():
print(x.relative_to(path.parent))