再次问好StackExchange!
尝试打印目录中的所有文件,但这次我只想打印文件名中某处包含字符串...“AMX_error”... csv的所有.csv文件。我有“所有.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..
在文件名中搜索字符串的符号是什么?谢谢!
答案 0 :(得分:1)
除非您有理由先过滤文件,否则只需在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)