How do I concatenate text files in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Concatenating text files in Python usually means reading multiple input files and writing their contents into one output file in order. The best approach depends on whether you care about memory usage, newline handling, encodings, and whether the files are plain text or structured data such as CSV.
The Straightforward Text Approach
For ordinary text files, a simple loop with open() is usually enough:
This is a good default because it:
- streams line by line instead of reading everything into memory
- makes the ordering explicit
- keeps encoding under your control
For most text-processing tasks, this is better than building one giant string with repeated + operations.
Concatenating Whole Files at Once
If the files are small and you value brevity over streaming, you can read each file completely:
This is readable, but it loads each entire file content into memory. That is usually fine for small config files or notes, but less appropriate for logs or large exports.
Copying More Efficiently
If you do not need to inspect lines and just want to copy text efficiently, shutil.copyfileobj() is a nice fit:
This keeps the code focused on file-to-file transfer instead of manual line handling.
Be Careful with Newlines
The biggest real-world problem is not concatenation itself. It is boundary formatting.
Suppose part1.txt does not end with a newline, while part2.txt begins immediately with text. A naive concatenation produces:
If you need a separator, add one deliberately:
The right separator depends on the data. For log files, you may want exactly one newline. For prose documents, you might want two. For machine-readable formats, you should preserve the format rules rather than insert arbitrary blank lines.
Concatenating CSV Is a Different Problem
If the files are CSV, JSON Lines, or another structured format, simple text concatenation may be wrong. CSV files often repeat headers, and concatenating them verbatim usually leaves duplicate header rows in the middle of the result.
For CSV, use the csv module or a table library:
That is not just concatenation. It is format-aware merging, which is usually what you really need for structured text.
Building a Reusable Function
If you do this often, wrap the logic in a small helper:
This keeps the call site short while still making separator behavior explicit.
Common Pitfalls
One common mistake is forgetting to specify encoding. If the input files are not all compatible with the default encoding on the current machine, you can get decoding errors or silently corrupted characters.
Another issue is reading every file into memory even when the files are large. Streaming line by line is safer and almost as simple.
Developers also often concatenate structured files as plain text. That may be acceptable for log chunks, but it is wrong for formats that contain headers or delimiters with semantic meaning.
Finally, be intentional about newlines. Concatenation bugs often show up at file boundaries, not inside the files themselves.
Summary
- For plain text, the simplest robust solution is to stream each input file into one output file.
- Use
encoding="utf-8"or another explicit encoding instead of relying on platform defaults. - Read whole files only when they are small and memory is not a concern.
- Add separators deliberately if file-boundary newlines matter.
- For CSV or other structured text formats, use format-aware parsing instead of raw concatenation.

