I am trying to merge and prepend a new line to each txt file using Python. So the goal is to merge all 200 text files into one text file and a new line TEXT,
to all 200 files as the first line of data for the text file. Should I prepend and then merge to make life easier?
I tried merging and prepending a new line to the existing txt file but when I open the result file, it deltes all the content in the file and adds a new line TEXT,
. Anyway I can fix this?
import glob
read_files = glob.glob("*.txt")
with open("result.txt", "wb") as outfile:
for f in read_files:
with open(f, "r") as infile:
outfile.write(infile.read())
with open(f,"w+") as infile:
infile.write("TEXT,"+infile.read())
with open(f,"r+") as infile:
b=infile.read()
print b
Actual Result:
TEXT,
Desired Result
TEXT,
……………(JUST RANDOM DATA)