2016-10-03 54 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循環來標識特定的文件名。這樣可行!應該能夠處理我的細微差別(按文件名)數據集。 –