'making folder and copying respective files to the respective directory

I have .txt files in a directory data and the structure of the files are as follows

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

I want to make subfolder as SXN13, SXN01,SXN11 just inside a folder "MAIN_DATA" so that final output would be(when file string matches folder name move the files to same folder)

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 

In this way i have to do the same process for thousands of files



Solution 1:[1]

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}')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 UnderMountain