Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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.




Method #1: Using os module

import os
# list the files in directory
lis = os.listdir('D:\\python'
tgt = os.listdir('D:\\python'
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)
    # check for if file exists
    # if yes delete the file 
    # otherwise data will be appended to existing file
    if len(tgt)>0:
        open(tgt, 'a').close()
        # create an empty file
        open(tgt, 'a').close()
    head = open('D:\\python'
                '\\data_files\\target_file\\master.txt', 'a+')
    line ='empno, ename, sal'
    # write header to output
    print(head, line)
    # below loop to write data to output file
    for line1 in lis:
        f_dir = file_dir+'\\'+line1
        # open files in read mode
        in_file = open(f_dir, 'r+')
        # open output in append mode
        w = open(out_file, 'a+')
        d = in_file.readline()
        d = in_file.readlines()
        for line2 in d:
        ct = ct + 1  


Method #2: Using pandas

import pandas as pd
# pd.read_csv creates dataframes
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]
# concat function concatenates the frames 
result = pd.concat(frames)
# to_csv function writes output to file
              '\\target_file\\master.txt', encoding ='utf-8', index = False)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!