Merging text files with python


Here is a simple yet effective program that will concatenate (or append) all text files using python.  Simply define the following three variables at the top of the program:

  1. File extension (example: .txt, .csv, etc…)
  2. Directory path:  If you are using Python under windows, then place an extra forward slash in your file path for every forward slash (ex: C:\\Documents and Settings\\user\\folder)
  3. The name of the results file that is to be created.  This can carry a different extension than the files you are concatenating
#set to the file extension of "to-be-merged" files
ext = '.txt'
#set to your working directory
dir_path = '/home/user/pywork'
#set to the name of your output file
results = 'final.out'

import os
files = os.listdir(dir_path)
for f in files:
  if f.endswith(ext):
    data = open(f)
    out = open(results, 'a')
    for l in data:
      print(l, file=out)
    data.close()
    out.close()

Leave a comment

Filed under Computer - Technical, Python

Comments are closed.