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()
标签:遍历,python,filelist,replace,re,names,file,path,目录 From: https://www.cnblogs.com/dengcongcong/p/16642184.html