Python | Write multiple files data to master file
Last Updated :
06 May, 2019
Given a number of input files in a source directory, write a Python program to read data from all the files and write it to a single master file.
Source directory contains n number of files, and structure is same for all files. The objective of this code is to read all the files one by one and then append the output into a single master file having structure same as source files.
Taking three input files as example, named emp_1.txt, emp_2.txt, emp_3.txt, output will contain data from all the input files.
Input:
Output:
Method #1: Using os module
import os
lis = os.listdir( 'D:\\python'
'\\data_files\\data_files' )
print (lis)
tgt = os.listdir( 'D:\\python'
'\\data_files\\target_file' )
file_dir = 'D:\\python\\data_files\\data_files'
out_file = r 'D:\\python\\data_files\\target_file\\master.txt'
ct = 0
print ( 'target file :' , tgt)
try :
if len (tgt)> 0 :
os.remove( 'D:\\python'
'\\data_files\\target_file\\master.txt' )
open (tgt, 'a' ).close()
else :
open (tgt, 'a' ).close()
except :
head = open ( 'D:\\python'
'\\data_files\\target_file\\master.txt' , 'a+' )
line = 'empno, ename, sal'
print (head, line)
head.close()
for line1 in lis:
f_dir = file_dir + '\\' + line1
in_file = open (f_dir, 'r+' )
w = open (out_file, 'a+' )
d = in_file.readline()
d = in_file.readlines()
w.write( "\n" )
for line2 in d:
print (line2)
w.write(line2)
ct = ct + 1
w.close()
|
Output:
Method #2: Using pandas
import pandas as pd
df1 = pd.read_csv( 'D:\python\data_files\data_files\emp_1.txt' )
df2 = pd.read_csv( 'D:\python\data_files\data_files\emp_2.txt' )
df3 = pd.read_csv( 'D:\python\data_files\data_files\emp_3.txt' )
frames = [df1, df2, df3]
result = pd.concat(frames)
result.to_csv( 'D:\\python\\data_files'
'\\target_file\\master.txt' , encoding = 'utf-8' , index = False )
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...