Solution for Why would a merged file be one less line than its input files?
is Given Below:
I’m doing some multithread webscraping and write 100 lines per thread into 100 files. I check to make sure every file has 100 lines then I merge. Then I check to make sure the merged file has 10000 lines. Only this time, the merged file seems to be one line short. All other runs have had 10000 lines. Any idea why a merged file would have one line less? Here’s the code I’m using to merge. I’ve checked that all input files have 100 lines. They all do. So I’m not sure why the merged file has one line less.
with open(rf"datarun12.txt", "w", encoding="utf-8") as outfile: for fname in filenames: with open(fname, "r", encoding = "utf-8") as infile: for line in infile: outfile.write(line)
This occurs when the very last line of the file isn’t a new line. One of my input files didn’t write a newline at the end, so I had 100 lines in every input file, but only 9999 lines in the output due to that lack of a newline character at the end of one input file causing its last line to merge with the first line of the adjacent input file.