You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从目录中使用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 the recursive=True flag: glob.glob("**/????-??-??.dat", recursive=True).
  • These patterns guarantee no overlap between the two lists—since the _patients suffix 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:32:40