0

Missing first line while reading from a file and transferring lines to a new one, wondering why. I have found a solution by adding x = flower1.readline() y = flower2.readline() and printing them on the file, but still did not understand why the first lines are missing at the first version of my code:

flower1 = open('/Users/berketurer/Desktop/week5_flower1.txt','r')
flower2 = open('/Users/berketurer/Desktop/week5_flower2.txt', 'r')
newFile = open('/Users/berketurer/Desktop/FlowerCombined.txt','w')

for line in flower1:
    lines = flower1.read()
    newFile.write(lines)

newFile.write('\n')

for line in flower2:
    lines = flower2.read()
    newFile.write(lines)

flower1.close()
flower2.close()
newFile.close()
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

1 Answers1

2

that loop makes no sense:

for line in flower1:
    lines = flower1.read()
    newFile.write(lines)

this iterates on the lines of the file, and inside the loop you read the whole file!

The fact that the first line is missing is because python has already consumed the line in the first iteration of for line in flower1. Then it reads the rest of the file and writes it, and the loop ends because the end of file has been reached... Note that line is never used in the code. Usually not a good sign.

just remove the loop:

lines = flower1.read()
newFile.write(lines)

If you want to process line by line for some reason (may come in handy if you want to filter out some lines) keep the loop but don't read in the loop:

for line in flower1:
    newFile.write(line)

There's also this one-liner: newFile.writelines(flower1) if you don't need filtering.

There are better ways to concatenate general files together though (binary or text). I like the version which use shutil.copyfileobj because even if the files are huge, the memory footprint is moderate, and performance-wise since blocksize is larger than 1 line, the speed may be slightly better.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • I understood why the loop unnecessary is, but still didn't full get why the first line is missing. I mean what does consuming the file exactly mean? Thanks – Berke Turer Apr 10 '20 at 08:42
  • the loop iterates on the file line by line. It reads the first line and puts it in `line` ... which you're not using. The next read just reads the _rest_ of the file. – Jean-François Fabre Apr 10 '20 at 08:47