[Python] What is the Python way to walk a directory tree?

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    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):
    files += [item]
    elif isdir(contents):
    folders += [item]
    return files, folders

    Continue reading...

Compartilhe esta Página