0
点赞
收藏
分享

微信扫一扫

python读取一个文件夹和子文件夹目录

RIOChing 2022-11-23 阅读 136


问题:
读取文件夹内容

文件处理所用到的函数

os.path.isdir(dir_path):#判断路径是否是文件夹
os.access(save_path,os.F_OK) #判断文件,F_OK文件,W_OK可写,R_OK可读
os.path.exists(save_path) #判断文件夹或者文件

file_name = os.listdir(dir_path):#返回路径文件夹中的所有文件名数组
int( os.path.splitext(filename)[0] ) #splitext将文件名跟后缀分开,转化为int名
name.sort() #升序排序

name = "256.json"
int(name[:-5]) #文件名字 返回256
os.path.join(new_path, file) #与new_path+file一样

遍历整个文件夹和子文件夹内容

# 得到csv中所有的swap路径文件csv
def get_all_files(path, find_end = ".csv", is_end = True):
print("开始读取所有后缀为",find_end, " 的文件, 返回相对路径")
csv_files = []
for root,dirs,files in os.walk(path):
for file in files:
if is_end:
if file.endswith(find_end):
csv_files.append(os.path.join(root, file))
else:
if file.startswith(find_end):
csv_files.append(os.path.join(root, file))
print(csv_files[:5])
return csv_files


举报

相关推荐

0 条评论