python 获取文件夹下所有的文件名 自动创建文件夹 判断文件是否存在

xiaoxiao2021-03-01  41

import os # 获取文件下的所有txt文件的路径 def file_name(self, user_dir): file_list = list() for root, dirs, files in os.walk(user_dir): for file in files: # if os.path.splitext(file)[1] == '.txt': file_list.append(os.path.join(root, file)) return file_list # # 延申 # 判断文件夹是否存在,若不存在,则自动创建文件夹 def confirm_folder(path): if not os.path.exists(path): os.makedirs(path) # 判断文件是否存在: os.path.isfile(file_name)

若打开if 则,只获取txt的文件名, 下一行就得缩进;

转载请注明原文地址: https://www.6miu.com/read-3450309.html

最新回复(0)