I need to read a sequence of files and fileinput
seemed just what was needed but then I realized it lacks a read
method.
- What is the canonical way (if any) to do this? (Explicit catenation will be wasteful.)
- Is there a known technical or security reason that
fileinput
does not supportread
?
This related question is not a duplicate.
Advertisement
Answer
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))