遍历目录树的Python方法是什么?

我觉得分配文件和文件夹以及执行+ = [item]部分有点笨拙。有什么建议么?我正在使用Python 3.2

from os import *
from os.path import *

def dir_contents(path):
    contents = listdir(path)
    files = []
    folders = []
    for i, item in enumerate(contents):
        if isfile(contents[i]):
            files += [item]
        elif isdir(contents[i]):
            folders += [item]
    return files, folders
评论
wquod
wquod

在搜寻相同的信息时,我发现了这个问题。

I am posting here the smallest, clearest code which I found at http://www.pythoncentral.io/how-to-traverse-a-directory-tree-in-python-guide-to-os-walk/ (rather than just posting the URL, in case of link rot).

该页面包含一些有用的信息,并且还指向其他一些相关页面。

# Import the os module, for the os.walk function
import os

# Set the directory you want to start from
rootDir = '.'
for dirName, subdirList, fileList in os.walk(rootDir):
    print('Found directory: %s' % dirName)
    for fname in fileList:
        print('\t%s' % fname)
点赞
评论
{孤心}
{孤心}

Take a look at the os.walk function which returns the path along with the directories and files it contains. That should considerably shorten your solution.

点赞
评论
唯1的唯一
唯1的唯一

确实使用

items += [item]

坏的原因有很多...

  1. The append method has been made exactly for that (appending one element to the end of a list)

  2. You are creating a temporary list of one element just to throw it away. While raw speed should not your first concern when using Python (otherwise you're using the wrong language) still wasting speed for no reason doesn't seem the right thing.

  3. You are using a little asymmetry of the Python language... for list objects writing a += b is not the same as writing a = a + b because the former modifies the object in place, while the second instead allocates a new list and this can have a different semantic if the object a is also reachable using other ways. In your specific code this doesn't seem the case but it could become a problem later when someone else (or yourself in a few years, that is the same) will have to modify the code. Python even has a method extend with a less subtle syntax that is specifically made to handle the case in which you want to modify in place a list object by adding at the end the elements of another list.

Also as other have noted seems that your code is trying to do what os.walk already does...

点赞
评论
Lily
Lily
def dir_contents(path):
    files,folders = [],[]
    for p in listdir(path):
        if isfile(p): files.append(p)
        else: folders.append(p)
    return files, folders
点赞
评论
旧言虐心
旧言虐心

如果要递归遍历所有文件,包括子文件夹中的所有文件,我相信这是最好的方法。

import os

def get_files(input):
    for fd, subfds, fns in os.walk(input):
       for fn in fns:
            yield os.path.join(fd, fn)

## now this will print all full paths

for fn in get_files(fd):
    print(fn)
点赞
评论
情话与狗
情话与狗

Try using the append method.

点赞
评论
wearum
wearum

I've not tested this extensively yet, but I believe this will expand the os.walk generator, join dirnames to all the file paths, and flatten the resulting list; To give a straight up list of concrete files in your search path.

import itertools
import os

def find(input_path):
    return itertools.chain(
        *list(
            list(os.path.join(dirname, fname) for fname in files)
            for dirname, _, files in os.walk(input_path)
        )
    )
点赞
评论
R幻影
R幻影

从Python 3.4开始,有了新的模块pathlib。因此,获取所有目录和文件可以执行以下操作:

from pathlib import Path

dirs = [str(item) for item in Path(path).iterdir() if item.is_dir()]
files = [str(item) for item in Path(path).iterdir() if item.is_file()]
点赞
评论
non_et
non_et

我使用内置的os.walk和os.path.walk,而不是从我在其他地方发现的这段代码中衍生的内容:

http://code.google.com/p/mylibs/source/browse/lib/Python/MyPyLib/DirectoryStatWalker.py

我在这里不会重复介绍它,但是它以递归方式遍历目录,并且效率很高且易于阅读。

点赞
评论