如何从目录中使用glob获取两类同日期前缀数据文件的独立文件名列表?
How to Use glob to Separate Two File Types
I get it, you need two distinct lists: one for your regular date-named .dat files, and another for the ones with the _patients suffix. Here's exactly how to do it with glob:
1. Fetch Regular Data Files (e.g., 2018-01-02.dat)
Use a glob pattern that matches the fixed YYYY-MM-DD date format followed directly by .dat.
For a flexible pattern that works for any valid date (using ? to match any single character in the date positions):
import glob regular_files = glob.glob("????-??-??.dat")
For a stricter pattern that only accepts digits in the date fields (using character classes):
regular_files = glob.glob("[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].dat")
2. Fetch Patient Data Files (e.g., 2018-01-02_patients.dat)
Extend the previous pattern to include the _patients suffix right before .dat:
Flexible version:
patient_files = glob.glob("????-??-??_patients.dat")
Strict digit-only version:
patient_files = glob.glob("[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]_patients.dat")
Quick Notes
- If your files live in a subdirectory, prepend the path to the pattern (e.g.,
"./data/????-??-??.dat"). - To search recursively through nested folders, add
**/at the start of the pattern and use therecursive=Trueflag:glob.glob("**/????-??-??.dat", recursive=True). - These patterns guarantee no overlap between the two lists—since the
_patientssuffix is unique to the second file type, you’ll get clean, separate results every time.
Content of the question originates from Stack Exchange, question author Ted
相关产品推荐
相关产品推荐

