Creating a textual summary at the end of an operation
本问题已经有最佳答案,请猛点这里访问。
我需要你的帮助。我创建了一个python 2.7脚本来移动一些文件。现在,我要做的是在程序结束时做一个"摘要",说明移动了哪些文件以及在哪里。不过,这个总结必须以某种方式带有"识别"。我来告诉你我的意思:
1 2 3 4 5 6 7 8 9 10 11 12 13 | - Folder A | |------- File 1 |------- File 2 |------- File 3 -Folder B | |------- Sub Folder B1 | |-------- File 1 |-------- File 2 |---------File X.. |
我如何才能在Python中实现类似的功能?
非常感谢!
编辑:
好的,下面是解决方案:
1 2 3 4 5 6 7 8 9 10 | import os def list_files(startpath): for root, dirs, files in os.walk(startpath): level = root.replace(startpath, '').count(os.sep) indent = ' ' * 4 * (level) print('{}{}/'.format(indent, os.path.basename(root))) subindent = ' ' * 4 * (level + 1) for f in files: print('{}{}'.format(subindent, f)) |
谢谢您!
活动状态配方上有一个脚本,它执行以下操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #! /usr/bin/env python # tree.py # # Written by Doug Dahms # # Prints the tree structure for the path specified on the command line from os import listdir, sep from os.path import abspath, basename, isdir from sys import argv def tree(dir, padding, print_files=False): print padding[:-1] + '+-' + basename(abspath(dir)) + '/' padding = padding + ' ' files = [] if print_files: files = listdir(dir) else: files = [x for x in listdir(dir) if isdir(dir + sep + x)] count = 0 for file in files: count += 1 print padding + '|' path = dir + sep + file if isdir(path): if count == len(files): tree(path, padding + ' ', print_files) else: tree(path, padding + '|', print_files) else: print padding + '+-' + file def usage(): return '''Usage: %s [-f] <PATH> Print tree structure of path specified. Options: -f Print files as well as directories PATH Path to process''' % basename(argv[0]) def main(): if len(argv) == 1: print usage() elif len(argv) == 2: # print just directories path = argv[1] if isdir(path): tree(path, ' ') else: print 'ERROR: \'' + path + '\' is not a directory' elif len(argv) == 3 and argv[1] == '-f': # print directories and files path = argv[2] if isdir(path): tree(path, ' ', True) else: print 'ERROR: \'' + path + '\' is not a directory' else: print usage() if __name__ == '__main__': main() |