[Solved] read multiple text files and write to one file in one column [closed]


Your question is very vague, but I came up with this:

These are my assumptions:

  1. The “values” in each line of the text file are separated by whitespace
  2. The output file will contain values in the order in which the files they are contained in were passed in. So, if the function’s params were f1, f2, f3 and outfilepath, then the values in f1 appear first in outfilepath and the values in f2 appear second in outfilepath, etc

Here is a function that does what you want under these assumptions:

def readwrite(infilepaths, outfilepath):
    with open(outfilepath, 'w') as outfile:
        outfile.write(threeLinesOfHeader + '\n')
        for infilepath in infilepaths:
            with open(infilepath) as infile:
                skipLines = 4
                for _ in range(skipLines):
                    infile.readline()
                values = itertools.chain.from_iterable(line.strip().split() for line in infile)
                outfile.write('\n'.join(values) + '\n')
    print 'done'

Hope this helps

1

solved read multiple text files and write to one file in one column [closed]