定义函数,可直接调用:
主函数输入,直接打印即可。
def get_one_type_file_list(InputFilePath, FileSuffix):
# input: 文件夹路径(当前路径直接写路径名,
# 自动去除路径名 最前面 和 最后面 多个连续的/ or \
# FileSuffix:文件后缀,例如"csv","xlsx"
# 中英文路径和文件名都可以使用
# return: 在路径InputFilePath下,符合FileSuffix后缀的所有文件的列表,
# return:带输入路径的文件名,纯文件名
TempPath = InputFilePath
while ((TempPath[0] == '/') or (TempPath[0] == '\\')):
InputFilePath = TempPath[1:] TempPath = InputFilePath
while ((TempPath[-1] == '/') or (TempPath[-1] == '\\')):
InputFilePath = TempPath[:-2] TempPath = InputFilePath
FileList = [] PathList = [] FileNames = os.listdir(InputFilePath)
FindStr = r".*?\." + FileSuffix + "$" # 文件名末尾匹配 .+文件名后缀
if (len(FileNames) > 0):
for fn in FileNames:
result = re.match(FindStr, fn)
if result != None:
strTemp = InputFilePath + "\\" + fn
PathList.append((InputFilePath + "\\" + fn))
FileList.append(fn)
if (len(FileList) > 0):
print("get_one_type_file_list(): has ", len(FileList), ' ', FileSuffix, " file: \n")
for i in range(len(PathList)):
print(PathList[i])
print("\n\n")
return (PathList, FileList)
'''
---------------------------------测试---------------------------------
'''
if __name__ == "__main__":
strInputDir = r"C:\Users\Administrator\Desktop\shuju\3\step2\mqtt_data"
strFileSuffix = "csv"
lstFilePath, lstFileName = get_one_type_file_list(strInputDir, strFileSuffix)
作者:Rachelllllll