python遍历目录下的文件
import os, re file_path = “path” def readfile(): filelist = os.listdir(file_path) for i in filelist: filelist = str(os.path.splitext(i)[0]) #replace只能替换指定字符串,re可以使用正则表达式 # file_names = filelist.replace("-2", "").replace("-1", "") file_names = re.sub("-1|-2|-F5|-fw|-juniper|-radware|-radware-fw", '', filelist) print(file_names) readfile()