Skip to content
Advertisement

How to read a sequence of files as one file (fileinput lacks read)?

I need to read a sequence of files and fileinput seemed just what was needed but then I realized it lacks a read method.

  1. What is the canonical way (if any) to do this? (Explicit catenation will be wasteful.)
  2. Is there a known technical or security reason that fileinput does not support read?

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))
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement