Home > database >  Copying batch image files from sub-folders to parent folders
Copying batch image files from sub-folders to parent folders

Time:07-15

In one directory there are several folders that their names are as follows: 301, 302, ..., 600. Each of these folders contain two folders with the name of A and B. I need to copy all the image files from A folders of each parent folder to the environment of that folder (copying images files from e.g. 600>A to 600 folder) and afterwards removing A and B folders of each parent folder. I found the solution from this post but I don't know how to copy the files into parent folders instead of sub-folder and also how to delete the sub-folders after copying and doing it for several folders.

import shutil
import os, sys

exepath = sys.argv[0]

directory = os.path.dirname(os.path.abspath(exepath)) "\\Files\\"

credit_folder = os.path.dirname(os.path.abspath(exepath)) "\\Credits\\" 

os.chdir(credit_folder)
os.chdir(directory)

Source = credit_folder
Target = directory

files = os.listdir(Source)
folders = os.listdir(Target)

for file in files:
    SourceCredits = os.path.join(Source,file)

    for folder in folders:
        TargetFolder = os.path.join(Target,folder)

        shutil.copy2(SourceCredits, TargetFolder)
 print(" \n ===> Credits Copy & Paste Sucessfully <=== \n ")

CodePudding user response:

@hellohawii gave an excellent answer. Following code also works and you only need change value of Source when using.

import shutil
import os, sys
from tqdm import tqdm

exepath = sys.argv[0]  # current path of code

Source = os.path.dirname(os.path.abspath(exepath)) "\\Credits\\"  # path of folders:301, 302... 600

# Source = your_path_of_folders

files = os.listdir(Source)  # get list of folders under 301 etc, in your situation: [A, B]


def get_parent_dir(path=None, offset=-1):
    """get parent dir of current path"""
    result = path if path else __file__
    for i in range(abs(offset)):
        result = os.path.dirname(result)
    return result


def del_files0(dir_path):
    """delete full folder"""
    shutil.rmtree(dir_path)


for file_path in files:
    current_path = os.path.join(Source, file_path)  # current_path
    if file_path == 'A':  # select the folder to copy
        file_list = os.listdir(current_path)  # get file_list of selected folder
        parent_path = get_parent_dir(current_path)  # get parent dir path, namely target path
        for file in tqdm(file_list):
            shutil.copy(file, parent_path)
    del_files0(current_path) # delete current path(folder)
print(" \n ===> Credits Copy & Paste & delete Successfully <=== \n ")

CodePudding user response:

I recommend you to use the Pathlib.

from pathlib import Path
import shutil
from tqdm import tqdm

folder_to_be_sorted = Path("/your/path/to/the/folder")

for folder_named_number_i in tqdm(list(folder_to_be_sorted.iterdir())):
    # folder_named_number_i is 301, 302, ..., 600
    A_folder = folder_named_number_i / "A"
    B_folder = folder_named_number_i / "B"

    # move files
    for image_i in A_folder.iterdir():
        shutil.move(str(image_i), folder_named_number_i)

    # remove directories
    shutil.rmtree(str(A_folder))
    shutil.rmtree(str(B_folder))

The os.path is a more low-level module. I post another version here since you are using the os module in your question.

import shutil
import os
from tqdm import tqdm

folder_to_be_sorted = "/your/path/to/the/folder"

for folder_named_number_name in tqdm(os.listdir(folder_to_be_sorted)):
    folder_named_number_i = os.path.join(folder_to_be_sorted, folder_named_number_name)
    # folder_named_number_i is 301, 302, ..., 600
    A_folder = os.path.join(folder_named_number_i, "A")
    B_folder = os.path.join(folder_named_number_i, "B")

    # move files
    for image_i_name in os.listdir(A_folder):
        image_i = os.path.join(A_folder, image_i_name)
        shutil.move(str(image_i), folder_named_number_i)

    # remove directories
    shutil.rmtree(str(A_folder))
    shutil.rmtree(str(B_folder))

By the codes above I suppose you want to transfrom

# /your/path/to/the/folder
# │
# └───301
# │   │
# │   └───A
# │   │   └───image_301_A_1.png
# │   │   └───image_301_A_2.png
# │   │   └───image_301_A_3.png
# │   │   └───...(other images)
# │   │
# │   └───B
# │       └───image_301_B_1.png
# │       └───image_301_B_2.png
# │       └───image_301_B_3.png
# │       └───...(other images)
# │
# └───302(like 301)
# :
# :
# └───600(like 301)

to:

# /your/path/to/the/folder
# │
# └───301
# │   │
# │   └───image_301_A_1.png
# │   └───image_301_A_2.png
# │   └───image_301_A_3.png
# │   └───...(other images in folder 301/A/)
# │
# └───302(like 301)
# :
# :
# └───600(like 301)
  • Related