使用 glob.glob() 读取时如何排除某些文件?

How to exclude some files when reading with glob.glob()?

我正在使用 glob.glob() 读取一些文件。我想读取名称为 123*.txt 的所有文件,但名称为 123*error.txt 的文件除外。另外,有没有办法在 pd.concat()?

内的 for 循环中打印文件名
fields = ['StudentID', 'Grade']
path= 'C:/script_testing/'

parse = lambda f: pd.read_csv(f, usecols=fields)
table3 = pd.concat(
[parse(f) for f in glob.glob('C:/script_testing/**/*.txt', recursive=True)]
).pipe(lambda d: pd.crosstab(d.StudentID, d.Grade))

使用这个模式

files = glob.glob('C:/script_testing/**/123*[!error].txt`, recursive=True)

然后继续

fields = ['StudentID', 'Grade']
path= 'C:/script_testing/'

parse = lambda f: pd.read_csv(f, usecols=fields)
table3 = pd.concat(
    [parse(f) for f in files]
).pipe(lambda d: pd.crosstab(d.StudentID, d.Grade))

参考this post