简体   繁体   English

考虑到文件名包含 python 中目标文件夹的名称,如何将文件从一个文件夹复制到另一个文件夹

[英]How to copy files from one folder to another taking into account that the file name contains the name of the destination folders in python

I need to copy files from a source folder like this:我需要从这样的源文件夹中复制文件:

在此处输入图像描述

And paste them into another folders like these:并将它们粘贴到另一个文件夹中,如下所示:

在此处输入图像描述

As you can see, the destination folder has subfolders with the 1.1 and 1.2 numerations.如您所见,目标文件夹中包含编号为 1.1 和 1.2 的子文件夹。 (The real folder has too many, for that reason is not possible to do a manual "if" comparison) (真实文件夹太多,因此无法进行手动“如果”比较)

What I need is the python script walk in the Source folder and evaluate each file to determinate in which destiny folder and subfolder it have to put in.我需要的是 python 脚本在 Source 文件夹中走动并评估每个文件以确定它必须放入哪个 destination 文件夹和子文件夹。

For example the files "ABC_1.1_files.zip" and "ABC_1.1_test.xlsx" have to paste into folder "ABC" and in subfolder "1.1" and the same with the another files.例如,文件“ABC_1.1_files.zip”和“ABC_1.1_test.xlsx”必须粘贴到文件夹“ABC”和子文件夹“1.1”中,其他文件也一样。

I really appreciate your help!非常感谢您的帮助!

import os
import shutil

src = "PATH/TO/SOURCE/FOLDER"
dst = "PATH/TO/DESTINATION/FOLDER"

for file_name in os.listdir(src):
    sub, num = file_name.split("_")[:2]
    new_dst = dst + f"{sub}/{num}/"
    os.makedirs(os.path.dirname(new_dst), exist_ok=True)
    shutil.copy(src, new_dst)

暂无
暂无

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

相关问题 如何根据文件夹名称将文件从一个带有子文件夹的目录复制到另一个带有子文件夹的目录? - How do I copy files from one directory with sub-folders to another directory with sub-folders based on folder name? 将具有特定名称的文件从一个文件夹复制到 python 中的另一个文件夹 - Copy file with specific name from one folder to another in python 有没有办法使用 python 中该文件名的子文本将文件从一个文件夹复制到另一个文件夹? - Is there a way to copy files from one folder to another using a subtext of that file name in python? python - 仅当目标中不存在文件时,如何将文件从一个目录复制到另一个目录? - python - How to copy files from one directory to another only if the file is not present in destination? 如何将多个文件夹中的相似命名文件合并为每个文件名的一个文件夹 - How to combine similar named files from multiple folders into one folder for each file name Python:将相同的.csv文件从各个文件夹(每个文件夹有一个.csv文件)复制到一个文件夹中 - Python: Copy identical .csv files from various folders (each folder has one .csv file) into a single folder 如何根据包含文件夹名称的文件将文件复制到文件夹中? - How do I copy files into folders based on the file containing the folder name? Python:比较两个文件夹并将具有相同名称的文件从1个文件夹移动到另一个文件夹 - Python : Compare two folders and move files with the same name from 1 folder to another 如何根据文件名将一个文件夹中的多个文件分成不同的文件夹? - How can I separate many files from one folder into separate folders based on the name of the files? 如何将从 txt 文件中选择的文件复制到另一个文件夹 python - How to copy files selected from a txt file to another folder python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM