簡體   English   中英

打印出整個目錄樹

[英]Print out the whole directory tree

我現在的代碼:

import os

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

這不會打印出子目錄中的文件。 新文件夾是一個子目錄)。

我的問題是,我怎樣才能output目錄下的所有文件和子目錄下的文件呢?

import os 
def Test1(rootDir): 
    list_dirs = os.walk(rootDir) 
    for root, dirs, files in list_dirs: 
        for d in dirs: 
            print os.path.join(root, d)      
        for f in files: 
            print os.path.join(root, f) 

或者:

import os 
def Test2(rootDir): 
    for lists in os.listdir(rootDir): 
        path = os.path.join(rootDir, lists) 
        print path 
        if os.path.isdir(path): 
            Test2(path)

對於測試文件樹:

E:\TEST 
│--A 
│  │--A-A 
│  │  │--A-A-A.txt 
│  │--A-B.txt 
│  │--A-C 
│  │  │--A-B-A.txt 
│  │--A-D.txt 
│--B.txt 
│--C 
│  │--C-A.txt 
│  │--C-B.txt 
│--D.txt 
│--E 

運行以下代碼:

Test1('E:\TEST') 
print '=======================================' 
Test2('E:\TEST') 

您可以看到結果之間存在差異:

>>>  
E:\TEST\A 
E:\TEST\C 
E:\TEST\E 
E:\TEST\B.txt 
E:\TEST\D.txt 
E:\TEST\A\A-A 
E:\TEST\A\A-C 
E:\TEST\A\A-B.txt 
E:\TEST\A\A-D.txt 
E:\TEST\A\A-A\A-A-A.txt 
E:\TEST\A\A-C\A-B-A.txt 
E:\TEST\C\C-A.txt 
E:\TEST\C\C-B.txt 
======================================= 
E:\TEST\A 
E:\TEST\A\A-A 
E:\TEST\A\A-A\A-A-A.txt 
E:\TEST\A\A-B.txt 
E:\TEST\A\A-C 
E:\TEST\A\A-C\A-B-A.txt 
E:\TEST\A\A-D.txt 
E:\TEST\B.txt 
E:\TEST\C 
E:\TEST\C\C-A.txt 
E:\TEST\C\C-B.txt 
E:\TEST\D.txt 
E:\TEST\E 
>>> 

將它們保存在列表中:

import os 
files = []
def Test1(rootDir):
    files.append(rootDir)
    list_dirs = os.walk(rootDir) 
    for root, dirs, files in list_dirs: 
        for d in dirs: 
            files.append(os.path.join(root, d))      
        for f in files: 
            files.append(os.path.join(root, f))

import os 
files = [rootDir]
def Test2(rootDir):
    for lists in os.listdir(rootDir): 
        path = os.path.join(rootDir, lists) 
        files.append(path) 
        if os.path.isdir(path): 
            Test2(path)

Python Cookbook上的配方 577091中,您可以在那里使用或學習TREE 模擬器

import sys, os

FILES = False

def main():
    if len(sys.argv) > 2 and sys.argv[2].upper() == '/F':
        global FILES; FILES = True
    try:
        tree(sys.argv[1])
    except:
        print('Usage: {} <directory>'.format(os.path.basename(sys.argv[0])))

def tree(path):
    path = os.path.abspath(path)
    dirs, files = listdir(path)[:2]
    print(path)
    walk(path, dirs, files)
    if not dirs:
        print('No subfolders exist')

def walk(root, dirs, files, prefix=''):
    if FILES and files:
        file_prefix = prefix + ('|' if dirs else ' ') + '   '
        for name in files:
            print(file_prefix + name)
        print(file_prefix)
    dir_prefix, walk_prefix = prefix + '+---', prefix + '|   '
    for pos, neg, name in enumerate2(dirs):
        if neg == -1:
            dir_prefix, walk_prefix = prefix + '\\---', prefix + '    '
        print(dir_prefix + name)
        path = os.path.join(root, name)
        try:
            dirs, files = listdir(path)[:2]
        except:
            pass
        else:
            walk(path, dirs, files, walk_prefix)

def listdir(path):
    dirs, files, links = [], [], []
    for name in os.listdir(path):
        path_name = os.path.join(path, name)
        if os.path.isdir(path_name):
            dirs.append(name)
        elif os.path.isfile(path_name):
            files.append(name)
        elif os.path.islink(path_name):
            links.append(name)
    return dirs, files, links

def enumerate2(sequence):
    length = len(sequence)
    for count, value in enumerate(sequence):
        yield count, count - length, value

if __name__ == '__main__':
    main()

是另一個適用於 python3 的版本

示例 output:

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

代碼:

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)))

使用os.walk

>>> import os
>>> print(os.walk.__doc__)
Directory tree generator.

    For each directory in the directory tree rooted at top (including top
    itself, but excluding '.' and '..'), yields a 3-tuple
    ...

如果您想將打印的樹限制在某個給定的深度,可能是因為您有很多嵌套文件夾,那么您不想使用os.walk 相反,您希望在達到所需深度后立即停止遍歷嵌套文件夾。

from typing import Union, Optional
from pathlib import Path

def ptree(startpath: Union[str, Path], 
          max_depth:int = 1, 
          quick_glance: Optional[int] = None, 
          _current_depth:int = 0) -> None:
  """
  Recursively print directory tree up to a given `max_depth`, specifying if you
  like a limited number of files and dirs to include in a `quick_glance`.
  
  Parameters
  ----------
  startpath: Union[str, Path]
    The filepath at which to start.
  max_depth: int
    The maximum depth of nested directories to explore.
  quick_glance: Optional[int]
    If specified, limits exploration to the first however-many files and dirs.
  _current_depth: int
    So that we can track our depth as we call the function recursively.
  """
  
  if _current_depth==0:
    print(startpath)
  else:
    print(f'{"--"*_current_depth}{[d for d in startpath.split(os.sep) if d][-1]}')
  _current_depth += 1
  
  if _current_depth > max_depth:
    return None
  else:
    ls = os.listdir(startpath)
    files = [f for f in ls if os.path.isfile(os.path.join(startpath,f))]
    dirs = [d for d in ls if os.path.isdir(os.path.join(startpath,d))]
    
    if quick_glance:
      files = files[:quick_glance]
      dirs = dirs[:quick_glance]

    [print(f'{".."*_current_depth}{f}') for f in files]

    [ptree(os.path.join(startpath, d), max_depth, quick_glance, _current_depth)
       for d in dirs]
    
    return None

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM