• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Concatenate Multiple Files Together in Python

Concatenate Multiple Files Together in Python

June 24, 2022 Leave a Comment

You can concatenate files in Python easily. To concatenate two files, you can read the content from both files, store the contents in strings, concatenate the strings and then write the final string to a new file.

with open('file1.txt') as f:
    f1 = f.read()

with open('file2.txt') as f:
    f2 = f.read()

f3 = f1 + "\n" + f2

with open('file3.txt','w') as f:
    f.write(f3)

When working with files in Python, the ability to change or manipulate the contents of these files can be useful.

One such operation is being able to combine two files into one.

You can easily combine two files in Python.

To concatenate two files, you can read the content from both files, store the contents in strings, concatenate the strings and then write the final string to a new file.

Below is a simple example showing you how to merge two files into one using Python.

with open('file1.txt') as f:
    f1 = f.read()

with open('file2.txt') as f:
    f2 = f.read()

f3 = f1 + "\n" + f2

with open('file3.txt','w') as f:
    f.write(f3)

How to Concatenate Multiple Files in Python

If you have more than two files, or want to create a function for a variable number of files, you can do the following.

First, we can create a list of strings where each element contains the contents of each file. Then, you can join the elements of the strings together with join() and a newline character.

Below is a function which will allow you to append more than two files together in Python.

def concat_files(filenames, outfile):
    contents = []
    for filename in filenames:
        with open(filename) as f:
            contents.append(f.read())
    with open(outfile,'w') as f:
        f.write("\n".join(contents))

You can also open the output file and simply write each file’s contents to that file directly.

def concat_files(filenames, outfile):
    contents = []
    with open(outfile,'w') as out:
        for filename in filenames:
            with open(filename) as in:
                out.write(in.read())
            out.write("\n")

Hopefully this article has been useful for you to learn how to concatenate multiple files in Python.

Other Articles You'll Also Like:

  • 1.  Python Check if Attribute Exists in Object with hasattr() Function
  • 2.  Scroll Down Using Selenium in Python
  • 3.  Python Destroy Object – How to Delete Objects with del Keyword
  • 4.  Python isfinite() Function – Check if Number is Finite with math.isfinite()
  • 5.  Python cube root – Find Cube Root of Number With math.pow() Function
  • 6.  How to Add Commas to Numbers in Python
  • 7.  Cartesian Product of Two Lists in Python
  • 8.  Read Last N Lines of File in Python
  • 9.  Using Python to Remove Last Character from String
  • 10.  How to Iterate over Everything in Word Document using python-docx

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy