获取文件夹的子文件夹中所有文件的路径并保存在一个文本中,用python实现
时间: 2023-03-24 21:05:01 浏览: 107
可以使用os模块和递归函数来实现获取文件夹的子文件夹中所有文件的路径并保存在一个文本中的操作,具体代码如下:
import os
def get_all_files(path, file_list):
"""
递归获取文件夹中所有文件的路径
:param path: 文件夹路径
:param file_list: 文件路径列表
"""
if os.path.isdir(path):
for sub_path in os.listdir(path):
sub_path = os.path.join(path, sub_path)
get_all_files(sub_path, file_list)
else:
file_list.append(path)
def save_file_path(file_list, save_path):
"""
将文件路径保存到文本中
:param file_list: 文件路径列表
:param save_path: 保存路径
"""
with open(save_path, 'w') as f:
for file_path in file_list:
f.write(file_path + '\n')
if __name__ == '__main__':
path = 'your_folder_path'
save_path = 'your_save_path'
file_list = []
get_all_files(path, file_list)
save_file_path(file_list, save_path)
其中,path为要获取文件夹中所有文件的路径的文件夹路径,save_path为保存文件路径的文本路径。
阅读全文
相关推荐

















