Reputation: 1017
I have a directory that contains few sub directories. In each sub directory there are few files, and I'd like to get the full paths of the files that contain the word "raw" in their filename and that ends with ".txt" (in each directory there is a file with "raw" in it's name).
Anyone knows how to do it elegantly with python 3?
Upvotes: 2
Views: 1290
Reputation: 60944
We can use the Path.resolve
method to get the absolute path to the directory, then use Path.glob
to collect the Path
objects representing the paths of the files we want to retrieve.
from pathlib import Path
paths = [path for path in Path('path/to/dir').resolve().glob("**/*raw*.txt")]
Here your search is specified by **/*raw*.txt
. This will return some number of Path
objects (the exact type will depend on the operating system you use).
If you want them as strings, you can substitute [str(path) for path in ..]
above
Upvotes: 1