Home > Net >  making folder and copying respective files to the respective directory
making folder and copying respective files to the respective directory

Time:02-20

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

CodePudding user response:

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

CodePudding user response:

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

  • Related