制作文件夹并将相应文件复制到相应目录

making folder and copying respective files to the respective directory

我在目录data中有.txt文件,文件结构如下

data
    SN.SXN13.00.ABC.txt
    SN.SXN13.00.ABD.txt
    SN.SXN13.00.ABE.txt
    SN.SXN01.00.ABC.txt
    SN.SXN01.00.ABD.txt
    SN.SXN01.00.ABE.txt
    SN.SXN11.00.ABC.txt
    SN.SXN11.00.ABD.txt
    SN.SXN11.00.ABE.txt

我想在文件夹 "MAIN_DATA" 中创建子文件夹 SXN13, SXN01,SXN11 这样最终输出将是(当文件字符串匹配文件夹名称时将文件移动到同一文件夹)

MAIN_DATA
         SXN13
             SN.SXN13.00.ABC.txt
             SN.SXN13.00.ABD.txt
             SN.SXN13.00.ABE.txt 
         SXN01
             SN.SXN01.00.ABC.txt
             SN.SXN01.00.ABD.txt
             SN.SXN01.00.ABE.txt
         SXN11
             SN.SXN11.00.ABC.txt
             SN.SXN11.00.ABD.txt
             SN.SXN11.00.ABE.txt 

这样我必须对数千个文件执行相同的过程

import os, shutil

# Obtain all files in the data directory ending with .txt
# The filtering step might be unnecessary if you know all files are .txt
# In that case, you could also just do: files = os.listdir("data")
files = list(filter(lambda fn: fn.endswith(".txt"), os.listdir("data")))

# Obtain and create all possible subfolder names
for subfolder in set(map(lambda fn: fn.split(".")[1], files)):
    os.makedirs(os.path.join("MAIN_DATA", subfolder))

# Finally, copy files over corresponding subfolders
for file in files:
    subfolder = file.split(".")[1]
    original_file_path = os.path.join("data", file)
    new_file_path = os.path.join("MAIN_DATA", subfolder, file)
    shutil.copyfile(original_file_path, new_file_path)

# Remove the original data folder, proceed at your own risk
# shutil.rmtree("data")
import os
import shutil

from_path = 'data'
to_path = 'MAIN_DATA'

if not os.path.exists(to_path):
    os.mkdir(to_path)

for file in os.listdir(f"{from_path}"):
    dirname = file.split('.')[1]

    if not os.path.exists(f'{to_path}/{dirname}'):
        os.mkdir(f'{to_path}/{dirname}')
    shutil.copy2(f'{from_path}/{file}', f'{to_path}/{dirname}/{file}')