• 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
  • Ruby
  • 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.  Using Python to Remove Quotes from String
  • 2.  Writing Multiple Lines Lambda Expression in Python
  • 3.  How to Check if Number is Divisible by 3 in Python
  • 4.  Using Python to Capitalize Every Other Letter of String
  • 5.  Format Numbers as Dollars in Python with format()
  • 6.  Ceiling Function Python – Get Ceiling of Number with math.ceil()
  • 7.  Using Python Selenium Webdriver to Refresh Page
  • 8.  Python not in – Check if Value is Not Included in Object
  • 9.  Check if Number is Larger than N in Python
  • 10.  Symmetric Difference of Two Sets in Python

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy