What is the Python way to walk a directory tree?

2019-02-11 21:26发布

I feel that assigning files, and folders and doing the += [item] part is a bit hackish. Any suggestions? I'm using 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

9条回答
Ridiculous、
2楼-- · 2019-02-11 21:36

Since Python 3.4 there is new module pathlib. So to get all dirs and files one can do:

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()]
查看更多
相关推荐>>
3楼-- · 2019-02-11 21:39

Try using the append method.

查看更多
三岁会撩人
4楼-- · 2019-02-11 21:40

Instead of the built-in os.walk and os.path.walk, I use something derived from this piece of code I found suggested elsewhere:

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

I won't repaste it here, but it walks the directories recursively and is quite efficient and easy to read.

查看更多
Viruses.
5楼-- · 2019-02-11 21:42

If you want to recursively iterate through all the files, including all files in the subfolders, I believe this is the best way.

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)
查看更多
Animai°情兽
6楼-- · 2019-02-11 21:43

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)
        )
    )
查看更多
我想做一个坏孩纸
7楼-- · 2019-02-11 21:44
def dir_contents(path):
    files,folders = [],[]
    for p in listdir(path):
        if isfile(p): files.append(p)
        else: folders.append(p)
    return files, folders
查看更多
登录 后发表回答