Search code examples
pythonglobshutil

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

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