Reputation: 9620
I need to read a sequence of files and fileinput
seemed just what was needed but then I realized it lacks a read
method.
fileinput
does not support read
?This related question is not a duplicate.
Upvotes: 0
Views: 217
Reputation: 123473
Since they're going to all be read into memory at once, you can make use of the io.StringIO
class
import io
class Filelike:
def __init__(self, filenames):
self.data = io.StringIO()
for filename in filenames:
with open(filename) as file:
self.data.write(file.read())
self.data.seek(0) # Rewind.
def read(self):
return self.data.getvalue()
if __name__ == '__main__':
filenames = ['file1.txt', 'file2.txt', ...]
filelike = Filelike(filenames)
# process lines
for line in filelike.read().splitlines():
print(repr(line))
Upvotes: 1