您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

用Python编写生成树状结构的文件目录的脚本的教程

2025/11/30 14:15:43发布28次查看
有时候需要罗列下u盘等移动设备或一个程序下面的目录结构的需求。基于这样的需求个人整理了一个使用python的小工具,期望对有这方面需求的朋友有所帮助。以下为具体代码:
如果你所有要求的文件目录不需要完整的文件路径的话,直接更换下面的注释代码即可~
# -*- coding:utf-8 -*-import osdef list_files(startpath): filesave = open('list.txt','w') for root, dirs, files in os.walk(startpath): level = root.replace(startpath, '').count(os.sep) indent = ' ' * 1 * level #filesave.write('{}{}/'.format(indent, os.path.basename(root)) + '\n') filesave.write('{}{}\\'.format(indent, os.path.abspath(root)) + '\n') subindent = ' ' * 1 * (level + 1) for f in files: #filesave.write('{}{}'.format(subindent, f) + '\n') filesave.write('{}{}{}'.format(subindent, os.path.abspath(root), f) + '\n') filesave.close() dir = raw_input('please input the path:')list_files(dir)
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product