How can I find files and then loop over them in Python? -
i have bash script contains following code:
merge_root_files(){ output_file=""$(date -u "+%y-%m-%dt%h%m%s")"z_merged.root" list_of_files="$(find . -name \*.root -type f)" command="hadd "${output_file}"" current_file in ${list_of_files}; echo "found root file "${current_file}"" command=""${command}" "${current_file}"" done echo "merge root files output root file "${output_file}"" echo "merge command: ${command}" eval "${command}" } merge_root_files
you can see searches recursively files end in .root
, loops on files. how similar in python? imagine generating list of various found files full or relative paths , looping on list, i'm not sure how generate such list.
glob makes easy type of thing.
import os import glob # full path .root files root_files = glob.glob('/your_path/*.root') # root file names root_file_names = [os.path.basename(f) f in glob.iglob('/your_path/*.root')]
Comments
Post a Comment