[Python] Flatten complex directory structure in Python

Discussão em 'Python' iniciado por Stack, Agosto 24, 2025.

  1. Stack

    Stack Membro Participativo

    I want to move files from a complex directory structure to just one place. For example i have this deep hierarchy:

    foo/
    foo2/
    1.jpg
    2.jpg
    ...


    I want it to be:

    1.jpg
    2.jpg
    ...


    My current solution:

    def move(destination):
    for_removal = os.path.join(destination, '\\')
    is_in_parent = lambda x: x.find(for_removal) > -1
    with directory(destination):
    files_to_move = filter(is_in_parent,
    glob_recursive(path='.'))
    for file in files_to_move:
    shutil.move(file, destination)


    Definitions: directory and glob_recursive. Note, that my code only moves files to their common parent directory, not an arbitrary destination.

    How can i move all files from a complex hierarchy to a single place succinctly and elegantly?

    Continue reading...

Compartilhe esta Página