I am using below line of code to recursively find the folders which has a particular sub-file name endswith .log
.
glob.iglob(f"{ROOT}/**/*.log", recursive = True)
Now, this code gives me an iterator which has complete path. But I want to return only the folder names, not whole path.
If this is how my directory looks like:
<ROOT>/
├── a/
│ ├── a.log
├── b/
│ └── b.log
├── c/
│ ├── c.log
│ └── cd/
│ └── cd.log
I want only the folder names in list like this:
['a', 'b', 'c', 'c/cd']
Is there a way to get only folder names?
Use os.path.dirname()
to get the directory name from the filenames.
This is using a set comprehension to avoid duplicates.
dirs_where_there_are_logs = {
os.path.dirname(pth)
for pth
in glob.iglob(f"{ROOT}/**/*.log", recursive=True)
}
Following the discussion in the comments, if you want dirnames relative to ROOT
,
dirs_where_there_are_logs = {
os.path.relpath(os.path.dirname(pth), ROOT)
for pth
in glob.iglob(f"{ROOT}/**/*.log", recursive=True)
}