Reputation: 691
lets say I have a dir named root
from pathlib import Path
root = Path('./root')
print(sorted(root.glob('*')))
Output:
[WindowsPath('G:/root/0'),
WindowsPath('G:/root/1'),
WindowsPath('G:/root/2'),
WindowsPath('G:/root/3')]
I want to exclude dir WindowsPath('G:/root/0')
& get this output using glob()
[WindowsPath('G:/root/1'),
WindowsPath('G:/root/2'),
WindowsPath('G:/root/3')]
I would also like to know how to exclude multiple such dirs using glob()
say this time I want to exclude WindowsPath('G:/root/0')
& WindowsPath('G:/root/3')
If I cannot do this using glob()
, other suggestions will be appreciated.
Upvotes: 1
Views: 2072
Reputation: 1489
Path.glob()
can not exclude paths (see documentation).
But you can filter the results afterwards, e.g. by using a list comprehension:
from pathlib import Path
root = Path('./root')
all_paths = sorted(root.glob('*'))
exclude_paths = [WindowsPath('G:/root/0'), WindowsPath('G:/root/3')]
filtered_paths = [path for path in all_paths if path not in exclude_paths]
Upvotes: 2