Combine multiple files into a single stream with richer metadata
Published in · 7 min read · Jun 13, 2021
--
Reading text files in Python is relatively easy to compare with most of the other programming languages. Usually, we just use the “open()” function with reading or writing mode and then start to loop the text files line by line.
This is already the best practice and it cannot be any easier ways. However, when we want to read content from multiple files, there is definitely a better way. That is, using the “File Input” module that is built-in to Python. It combines the content from multiple files that allow us to process everything in a single for-loop and plenty of other benefits.
In this article, I’ll demonstrate this module with examples.
Let’s have a look at the “ordinary” way of reading multiple text files using the open()
function. But before that, we need to create two sample files for demonstration purpose.
with open('my_file1.txt', mode='w') as f:
f.write('This is line 1-1\n')
f.write('This is…