Friday, 15 July 2022

Copying batch image files from sub-folders to parent folders

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 ")


from Copying batch image files from sub-folders to parent folders

No comments:

Post a Comment