
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Python - Write multiple files data to master file
File handling is an important part of any web application.
Python has several functions for creating, reading, updating, and deleting files.
To write to an existing file, you must add a parameter to the open()function −
"a" − Append − will append to the end of the file
"w" − Write − will overwrite any existing content
Example
import os # list the files in directory 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: # check for if file exists # if yes delete the file # otherwise data will be appended to existing file if len(tgt)>0: os.remove('D:\python' '\data_files\target_file\master.txt') open(tgt, 'a').close() else: # create an empty file open(tgt, 'a').close() except: head = open('D:\python' '\data_files\target_file\master.txt', 'a+') line ='empno, ename, sal' # write header to output print(head, line) head.close() # 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() w.write("\n") for line2 in d: print(line2) w.write(line2) ct = ct + 1 w.close() #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 result.to_csv('D:\python\data_files' '\target_file\master.txt', encoding ='utf-8', index = False)
- Related Articles
- How to share common data among multiple Python files?
- How to merge multiple files into a new file using Python?
- How to spilt a binary file into multiple files using Python?
- Python Pandas- Create multiple CSV files from existing CSV file
- Write a Python code to read JSON data from a file and convert it to dataframe, CSV files
- How to write multiple lines in text file using Python?
- How to write binary data to a file using Python?
- Rename multiple files using Python
- How to open multiple files using a File Chooser in JavaFX?
- How to Append Contents of Multiple Files Into One File on Linux?
- C program to handle integer data files using file concepts
- How to rename multiple files recursively using Python?
- Write a Python program to export dataframe into an Excel file with multiple sheets
- Is it better to have one big JavaScript file or multiple light files?
- How to write data to .csv file in Java?

Advertisements