问题:
读取文件夹内容
文件处理所用到的函数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12os.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一样
遍历整个文件夹和子文件夹内容
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# 得到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
最后
以上就是欢喜玉米最近收集整理的关于python读取一个文件夹和子文件夹目录文件处理所用到的函数遍历整个文件夹和子文件夹内容的全部内容,更多相关python读取一个文件夹和子文件夹目录文件处理所用到内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复