Reputation: 359
I have Python list with filenames ("filename_mask_list"), and I need to search some root Directory and its Sub-directories to find files, which match my filenames, in order to copy them to some common directory.
I have used pathlib.Path.glob to serach recursively directories:
from pathlib import Path
filename_mask_list = ['DEU.FourSeasonsHotelsandResorts.csv', 'DEU.Hilton.csv', 'DEU.Hertz.csv']
for searched_file in filename_mask_list:
for searched_path in Path('C:\root_dir').glob('**/' + searched_file):
print(searched_path)
With above code, the found filename in printed "searched_path" is in lower case. If I use direct option to instead of passing element from list as search criteria the casing is preserved:
for searched_path in Path('C:\root_dir').glob('**/DEU*.csv'):
print(searched_path)
I need to preserve filename casing of copied files, because having it in lowercase will cause dependent processes to crash.
Upvotes: 1
Views: 192
Reputation: 12620
Using glob.glob
will give you the file names with the same case as in the search criteria. I don't know the reasons for this apparent inconsistency.
import glob
filename_mask_list = ['DEU.FourSeasonsHotelsandResorts.csv', 'DEU.Hilton.csv', 'DEU.Hertz.csv']
for searched_file in filename_mask_list:
for searched_path in glob.glob('C:\root_dir/**/' + searched_file, recursive=True):
print(Path(searched_path))
Note that I preserved your spelling 'C:\root_dir'
for clarity but it is most lilely wrong.
Upvotes: 1