Reputation: 2231
I am trying to print the contents of a file. I have a file maze.txt
with the following contents:
7 7
1 1 R N E
1 2 B N W
1 3 B N N
And I am printing it using the following code:
with open(os.path.join('maze.txt')) as f:
for line in f:
print line
f.close()
However, my output has extra empty lines in between:
7 7
1 1 R N E
1 2 B N W
1 3 B N N
I've tried changing my print line to print line[0:-1]
, which works except it will cut off the last character in the final line because there's not a newline to get rid of after it. Is there an easy way to avoid this?
Upvotes: 0
Views: 91
Reputation: 5866
Just as the previous answer: when the print function doesn't end with a ',', then it adds a 'newline'.
Also, on your code, when opening a file with the 'with' code, you don't need to close the file: it's closed automatically when exiting the 'with' chunk of code.
Upvotes: 1