鱼C论坛

 找回密码
 立即注册
查看: 1096|回复: 0

[作品展示] 树形打印目录结构

[复制链接]
发表于 2021-8-1 17:20:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
CMD中没有tree这个命令,所以自己使用Python编写了一个可以遍历目录而且还是以树形图表的形式显示目录结构的脚本,
代码如下:
  1. '''If a directory is like this:
  2. root/
  3. |-- dir1/
  4. |   |-- file1.txt
  5. |   |-- file2.txt
  6. |   |-- sub1/
  7. |   |   |-- video1.mp4
  8. |   |   |-- video2.mp4
  9. |   |   |-- video3.mp4
  10. |   |   `-- sub_n/
  11. |   |       |-- f1.png
  12. |   |       `-- f2.png
  13. |   `-- sub2/
  14. |       |-- pic1.jpg
  15. |       |-- pic2.jpg
  16. |       `-- pic3.jpg
  17. |-- dir2/
  18. |   |-- text1.md
  19. |   |-- text2.md
  20. |   `-- text3.md
  21. |-- main.py
  22. |-- spam.py
  23. |-- eggs.py
  24. `-- hams.py

  25. Each row has three components: prefix, item and suffix.
  26. Definitions are as follow:

  27. prefix:
  28.     A prefix is everything before a file or a directory and it
  29.     also has three parts: upper prefix, indent and item mark.
  30.     * A upper prefix is a prefix before a directory, which will be
  31.       used to create new prefix for items within a directory.
  32.       Root directory's prefix is an empty string.
  33.     * Indent is either '|   ' or '    '.
  34.     * Item mark is adjacent to a file or a directory,
  35.       which is '`-- ' for last item or '|-- ' for everything except
  36.       the last one in a direcotry.

  37. item:
  38.     An item is either a directory or a file.

  39. suffix:
  40.     Only a directory has a suffix '/'.
  41. '''

  42. from os import listdir
  43. from os.path import abspath, basename, isdir, join

  44. MARK_1 = '    '  # indentation
  45. MARK_2 = '|   '  # indentation
  46. MARK_3 = '|-- '  # mark adjacent to a file or a directory
  47. MARK_4 = '`-- '  # mark for the last item of a directory


  48. def tree(dir_: str, prefix: str = '', indent: str = '') -> None:
  49.     '''Iter recursively a directory to print everything within it in
  50.     an ascii tree graph.

  51.     :param dir_: str, a root directory.
  52.     :param prefix: str, everything before a file or a directory.
  53.     :param indent: str, indent mark for building new prefix.
  54.     '''

  55.     # print directory
  56.     print(f'{prefix}{basename(abspath(dir_))}/')

  57.     # [upper prefix] [indent] |--
  58.     prefix = prefix[:-len(MARK_3)] + indent + MARK_3

  59.     contents = listdir(dir_)
  60.     max_indx = len(contents) - 1
  61.     for idx, item in enumerate(contents):
  62.         path = join(dir_, item)
  63.         if isdir(path):
  64.             if idx < max_indx:
  65.                 tree(path, prefix, MARK_2)
  66.             else:
  67.                 tree(path, prefix.replace(MARK_3, MARK_4), MARK_1)
  68.         else:
  69.             if idx == max_indx:
  70.                 prefix = prefix.replace(MARK_3, MARK_4)
  71.             # print file
  72.             print(f'{prefix}{item}')


  73. if __name__ == '__main__':
  74.     tree('.')
复制代码

使用的是递归,话说递归确实有点难啊
其他同学也可以试试用argparse这个标准库把代码改装成一个命令行工具
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-2 22:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表