How to Split a Text File in Python

09/18/2021
Contents
In this article, you will learn how to split a text file in Python.
How to Split a Text File
To split a text file in Python, you can follow these steps:
- Open the input file for reading.
- Open the output files for writing.
- Read lines from the input file and write them to the appropriate output file based on the split criteria.
- Close all the files.
Here’s some example code that splits a text file into multiple output files based on a given split criteria (in this example, every 1000 lines):
# Define the input and output file paths
input_file = "input.txt"
output_prefix = "output"
# Define the split criteria
lines_per_file = 1000
# Open the input file for reading
with open(input_file, "r") as f_in:
# Initialize the counter and output file index
count = 0
file_index = 1
# Open the first output file for writing
f_out = open(f"{output_prefix}_{file_index}.txt", "w")
# Loop through the input file
for line in f_in:
# Write the line to the current output file
f_out.write(line)
# Increment the counter
count += 1
# If the counter reaches the split criteria, close the current output file and open a new one
if count == lines_per_file:
f_out.close()
file_index += 1
f_out = open(f"{output_prefix}_{file_index}.txt", "w")
count = 0
# Close the last output file
f_out.close()
In this example, the input file is read line by line, and each line is written to an output file. When the counter reaches the split criteria (1000 lines), the current output file is closed and a new one is opened with a new file index. The process continues until the end of the input file is reached.