简体   繁体   English

os.walk但目录在顶部?

[英]os.walk but with directories on top?

I have some simple code to print out the structure of a directory. 我有一些简单的代码来打印出目录的结构。 My example directory ABC contains subdirectory A containing A.txt , a subdirectory Z containing Z.txt , and a file info.txt . 我的示例目录ABC包含包含A.txt子目录A ,包含Z.txt的子目录Z和文件info.txt In real use, this will be big collection of many files and nested directories. 在实际使用中,这将是许多文件和嵌套目录的大集合。

import os
topdir = 'ABC/'
for dirpath, dirnames, files in os.walk(topdir):
    print(os.path.join(dirpath))
    for name in files:
        print(os.path.join(dirpath, name))

The output is: 输出是:

ABC/
ABC/info.txt
ABC/A
ABC/A/A.txt
ABC/Z
ABC/Z/Z.txt

How can I make it so directories are processed/printed on the top? 如何制作,以便在顶部处理/打印目录? I want the output to replicate what I see in Windows Explorer, which displays directories first, and files after. 我希望输出复制我在Windows资源管理器中看到的内容,它首先显示目录,然后显示文件。

The output I want: 我想要的输出:

ABC/
ABC/A
ABC/A/A.txt
ABC/Z
ABC/Z/Z.txt
ABC/info.txt

Without storing all the files in a list and sorting that list in one way or the other, you could make a recursive function and first recurse to the next level of the directory structure before printing the files on the current level: 如果不将所有文件存储在列表中并以某种方式对该列表进行排序,则可以创建递归函数,并在打印当前级别的文件之前首先递归到目录结构的下一级:

def print_dirs(directories):
    try:
        dirpath, dirnames, files = next(directories)
        print(dirpath)              # print current path; no need for join here
        for _ in dirnames:          # once for each sub-directory...
            print_dirs(directories) # ... recursively call generator
        for name in files:          # now, print files in current directory
            print(os.path.join(dirpath, name))
    except StopIteration:
        pass

print_dirs(os.walk(topdir))

The same could also be done with a stack, but I think this way it's a little bit clearer. 堆栈也可以这样做,但我认为这样更清晰一些。 And yes, this will also store some directories in a list/on a stack, but not all the files but just as many as there are levels of nested directories. 是的,这也会将一些目录存储在列表/堆栈中,但不是所有文件,只是存在嵌套目录的级别。

Edit: This had a problem of printing any next directory on the generator, even if that's not a sub-directory but a sibling (or "uncle" or whatever). 编辑:这有一个打印生成器上任何下一个目录的问题,即使这不是一个子目录,而是一个兄弟(或“叔叔”或其他)。 The for _ in dirnames loop should fix that, making the recursive call once for each of the subdirectories, if any. for _ in dirnames循环应该修复它,为每个子目录(如果有的话)进行一次递归调用。 The directory itself does not have to be passed as a parameter as it will be gotten from the generator. 目录本身不必作为参数传递,因为它将从生成器获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM