markdown python,包,树

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown python,包,树相关的知识,希望对你有一定的参考价值。

## Python script to print package tree diagrame
**How to use**
```Bash
python tree.py [-f] folder_path

-f: if show file
```

```Python
#! /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()
```

**Output**
```Bash
type command as: python tree.py -f .

+-authentication/
  |
  +-.DS_Store
  |
  +-sittest/
    |
    +-tasks/
    | |
    | +-loginReport/
    |   |
    |   +-loginreportview/
    |     |
    |     +-CheckSacViewStructureTaskData.java
    |     |
    |     +-CheckSacViewStructureTask.java
    |
    +-features/
    | |
    | +-loginReport/
    |   |
    |   +-loginreportview/
    |     |
    |     +-ShowFullContentLoginReportIncludeUserInformationAndLoginTimePeriodImpl.java
    |     |
    |     +-show_full_content_logn_report_include_user_information_and_login_time_period.feature
    |
    +-actions/
      |
      +-loginReport/
```

### Refer
[Source From](http://code.activestate.com/recipes/217212-treepy-graphically-displays-the-directory-structur/)

以上是关于markdown python,包,树的主要内容,如果未能解决你的问题,请参考以下文章

markdown Python分发包到网络,打包

markdown Python库/包

markdown Python私有包依赖项的权威指南

markdown ImportError:没有名为'_tkinter'的模块,请安装python3-tk包

Python 包依赖树

markdown文件的目录树生成