Python 3 pandas 目录搜索文件名中的字符串

Python 3 pandas directory search for a string in filename

再次问好 StackExchange!

正在尝试打印目录中的所有文件,但这次我只想打印文件名中某处具有字符串 ..."AMX_error"...csv 的所有 .csv 文件。我有 "all .csv" 工作,但缺少那一点搜索逻辑。

import glob
import pandas as pd

path = r'C:\Users\Desktop\Experiment\'

#Following command to search for string in the filename
allFiles = glob.glob(path + "/*.csv") & (search filename 'AMX_error' = true)

for filename in allFiles:
    print(filename)

#rest of code..

在文件名中搜索字符串的表示法是什么?谢谢!

除非您有理由先过滤文件,否则您可以在 for 循环中简单地检查文件名中是否包含感兴趣的字符串。

import glob
import pandas as pd

path = r'C:\Users\Desktop\Experiment'

#Following command to search for string in the filename
allFiles = glob.glob(path + "/*.csv")

for filename in allFiles:
    if 'AMX_error' in filename:
        print(filename)