手机
当前位置:查字典教程网 >脚本专栏 >python >用Python编写生成树状结构的文件目录的脚本的教程
用Python编写生成树状结构的文件目录的脚本的教程
摘要:有时候需要罗列下U盘等移动设备或一个程序下面的目录结构的需求。基于这样的需求个人整理了一个使用Python的小工具,期望对有这方面需求的朋友...

有时候需要罗列下U盘等移动设备或一个程序下面的目录结构的需求。基于这样的需求个人整理了一个使用Python的小工具,期望对有这方面需求的朋友有所帮助。以下为具体代码:

如果你所有要求的文件目录不需要完整的文件路径的话,直接更换下面的注释代码即可~

# -*- coding:utf-8 -*- import os def 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)

【用Python编写生成树状结构的文件目录的脚本的教程】相关文章:

Python生成随机数的方法

python处理文本文件实现生成指定格式文件的方法

Python写的贪吃蛇游戏例子

python文件和目录操作函数小结

Python实现的生成自我描述脚本分享(很有意思的程序)

用Python和MD5实现网站挂马检测程序

python中使用百度音乐搜索的api下载指定歌曲的lrc歌词

Python open读写文件实现脚本

python读文件逐行处理的示例代码分享

python备份文件以及mysql数据库的脚本代码

精品推荐
分类导航