2016-10-03 91 views
0

再次您好StackExchange!Python 3熊猫目录搜索文件名中的字符串

试图打印一个目录中的所有文件,但这次我只想打印所有.csv文件的字符串......“AMX_error”... 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.. 

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

回答

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) 
+0

从未想过要使用if-then循环来标识特定的文件名。这样可行!应该能够处理我的细微差别(按文件名)数据集。 –