

如果你所有要求的文件目录不需要完整的文件路径的话,直接更换下面的注释代码即可~
# -*- 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)) + '
')
fileSave.write('{}{}\'.format(indent, os.path.abspath(root)) + '
')
subIndent = ' ' * 1 * (level + 1)
for f in files:
#fileSave.write('{}{}'.format(subIndent, f) + '
')
fileSave.write('{}{}{}'.format(subIndent, os.path.abspath(root), f) + '
')
fileSave.close()
dir = raw_input('please input the path:')
list_files(dir)
