简体   繁体   中英

How to recursively move all files inside subdirectories to main folder? [python]

I have a folder directories look somewhat like this:

C:/Documents/A350/a/1.png
                   /2.png
                  b/1.png
            /B777/a/1.png
            /B747/a/1.png
                   /2.png
                  b/1.png
                  c/1.png
                  d/1.png
                   /2.png

I want to move all png to the main folder ie Documents.

def recur(input_path):
    dir_list = os.listdir(input_path)
    for directory in dir_list:
        path_name = os.path.join(input_path, directory)
        p = pathlib.Path(path_name)
        if p.is_dir():
            input_path = path_name
            return recur(input_path)
    return input_path

I have some code to get the deepest path inside a folder, but i am not so sure how to use the recursive function to achieve what i wanted.

Any help would be really appreciated, thanks!!

Below program get all files recursively from parent directory and copies files to parent directory.

import os
import glob
import shutil

files_abs_paths = []

def get_all_files(parent_dir):
    files_n_folders = glob.glob(f'{parent_dir}/**')
    
    for fl_or_fldr in files_n_folders:
        if os.path.isdir(fl_or_fldr):
            folder = fl_or_fldr
            get_all_files(folder)
        else:
            file = fl_or_fldr
            files_abs_paths.append(file)

parent_dir = r"C:'/Documents"

# get all files recursively in parent dir
get_all_files(parent_dir)

# copies files to parent_dir
for fl in files_abs_paths:
    # gets file_name
    file_name = os.path.basename(fl)

    # create file in parent_dir
    new_file_loc = f'{parent_dir}/{file_name}'
    if os.path.exists(new_file_loc) is False:
        shutil.copyfile(fl, new_file_loc)

You can also get all the files from a folder tree using os.walk :

If you don't mind overwriting files with duplicate names:

from os import walk, rename
from os.path import join

def collect_files(root):
    for src_path, _, files in walk(root):
        if src_path != root:
            for name in files:
                rename(join(src_path, name), join(root, name))

If you want to add a number to the end of files with duplicate names:

from os import walk, rename
from os.path import join, splitext, exists

def collect_files(root):
    for src_path, _, files in walk(root):
        if src_path != root:
            for name in files:
                dst_name = name
                dst_name_parts = splitext(dst_name)
                file_num = 1
                while exists(join(root, dst_name)):
                    dst_name = '{}_{:0>3}{}'.format(dst_name_parts[0], file_num, dst_name_parts[1])
                    file_num += 1
                rename(join(src_path, name), join(root, dst_name))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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