Your question is very vague, but I came up with this:
These are my assumptions:
- The “values” in each line of the text file are separated by whitespace
- 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
andoutfilepath
, then the values inf1
appear first inoutfilepath
and the values inf2
appear second inoutfilepath
, 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]