简体   繁体   English

将批处理图像文件从子文件夹复制到父文件夹

[英]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 .在一个目录中有几个文件夹,它们的名称如下:301、302、...、600。每个文件夹都包含两个文件夹,名称分别为AB 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 eg 600>A to 600 folder) and afterwards removing A and B folders of each parent folder.我需要将所有图像文件从每个父文件夹的 A 文件夹复制到该文件夹​​的环境中(将图像文件从例如 600>A 复制到 600 文件夹),然后删除每个父文件夹的AB文件夹。 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 ")

@hellohawii gave an excellent answer. @hellohawii 给出了一个很好的答案。 Following code also works and you only need change value of Source when using.以下代码也有效,您只需要在使用时更改Source的值。

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

I recommend you to use the Pathlib .我建议您使用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. os.path是一个更底层的模块。 I post another version here since you are using the os module in your question.我在这里发布了另一个版本,因为您在问题中使用了os模块。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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