Print out the whole directory tree

后端 未结 4 550
眼角桃花
眼角桃花 2021-01-03 11:05

The code I have now:

import os

Tree = {}
Tree = os.listdir(\'Dir\')
>>> print(Tree)
[\'New Folder\', \'Textfile1.txt\', \         


        
4条回答
  •  耶瑟儿~
    2021-01-03 11:27

    Here is yet another version appropriate for python3

    Example output:

    pyvarstar/
    |-- .bashrc
    |-- README
    |-- vstars -> versions/vstars_20170804/ 
    |-- versions/ 
    |   |-- vstars_20170804/ 
    |   |   |-- lib/
    |   |   |   |-- vstars/ 
    |   |   |-- bin/ 
    |   |   |   |-- getcoords
    |   |   |   |-- find_burst
    

    The code:

    def realname(path, root=None):
        if root is not None:
            path=os.path.join(root, path)
        result=os.path.basename(path)
        if os.path.islink(path):
            realpath=os.readlink(path)
            result= '%s -> %s' % (os.path.basename(path), realpath)
        return result
    
    def ptree(startpath, depth=-1):
        prefix=0
        if startpath != '/':
            if startpath.endswith('/'): startpath=startpath[:-1]
            prefix=len(startpath)
        for root, dirs, files in os.walk(startpath):
            level = root[prefix:].count(os.sep)
            if depth >-1 and level > depth: continue
            indent=subindent =''
            if level > 0:
                indent = '|   ' * (level-1) + '|-- '
            subindent = '|   ' * (level) + '|-- '
            print('{}{}/'.format(indent, realname(root)))
            # print dir only if symbolic link; otherwise, will be printed as root
            for d in dirs:
                if os.path.islink(os.path.join(root, d)):
                    print('{}{}'.format(subindent, realname(d, root=root)))
            for f in files:
                print('{}{}'.format(subindent, realname(f, root=root)))
    

提交回复
热议问题