用Python编写生成树状结构的文件目录的脚本的教程
发布于 2015-05-06 19:56:50 | 165 次阅读 | 评论: 0 | 来源: 网友投递
Python编程语言Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。
有时候需要罗列下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编写一个简单的FUSE文件系统的教程
用Python编写一个简单的俄罗斯方块游戏的教程
用Python编写一个简单的Lisp解释器的教程
用Python编写脚本使IE实现代理上网的教程
用Python编写一个基于终端的实现翻译的脚本
用Python编写web API的教程
用Python编写简单的定时器的方法及示例代码
用Python编写一个每天都在系统下新建一个文件夹的脚本
用Python编写简单的微博爬虫
Python编写检测数据库SA用户的方法