如果你所有要求的文件目录不需要完整的文件路径的话,直接更换下面的注释代码即可~
# -*- 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)
