簡體   English   中英

將批處理圖像文件從子文件夾復制到父文件夾

[英]Copying batch image files from sub-folders to parent folders

在一個目錄中有幾個文件夾,它們的名稱如下:301、302、...、600。每個文件夾都包含兩個文件夾,名稱分別為AB 我需要將所有圖像文件從每個父文件夾的 A 文件夾復制到該文件夾​​的環境中(將圖像文件從例如 600>A 復制到 600 文件夾),然后刪除每個父文件夾的AB文件夾。 我從這篇文章中找到了解決方案,但我不知道如何將文件復制到父文件夾而不是子文件夾中,也不知道如何在復制多個文件夾后刪除子文件夾。

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 給出了一個很好的答案。 以下代碼也有效,您只需要在使用時更改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 ")

我建議您使用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))

os.path是一個更底層的模塊。 我在這里發布了另一個版本,因為您在問題中使用了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))

通過上面的代碼,我想你想轉換

# /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)

至:

# /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