在一个目录中有几个文件夹,它们的名称如下:301、302、...,600

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

推荐答案

我建议你用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)

Python相关问答推荐

管道冻结和管道卸载

使用@ guardlasses. guardlass和注释的Python继承

从spaCy的句子中提取日期

提取相关行的最快方法—pandas

mypy无法推断类型参数.List和Iterable的区别

Python逻辑操作作为Pandas中的条件

在Python中调用变量(特别是Tkinter)

为什么if2/if3会提供两种不同的输出?

如何创建引用列表并分配值的Systemrame列

循环浏览每个客户记录,以获取他们来自的第一个/最后一个渠道

不允许 Select 北极滚动?

Python将一个列值分割成多个列,并保持其余列相同

Python—在嵌套列表中添加相同索引的元素,然后计算平均值

read_csv分隔符正在创建无关的空列

将相应的值从第2列合并到第1列(Pandas )

如何在基于时间的数据帧中添加计算值

有什么方法可以在不对多索引DataFrame的列进行排序的情况下避免词法排序警告吗?

如何使用count()获取特定日期之间的项目

如何在开始迭代自定义迭代器类时重置索引属性?

基于符号和位置 Select 数据帧特定区域的毕达式方法