Reputation: 1109
In Windows, if I'm using Python's "for line in file" syntax to read the contents of a normal text file, the final line will not end with a newline character, but all the lines before it will. In Linux, this behavior seems to be different -- when I read a file line-by-line and print its contents, I always find that the final line ends with a newline character, even if one didn't exist in the real file! I've also tried using the file.readline() function, with identical results.
(I'm assuming that the text file does actually not end in a newline. If it does, Linux prints two newlines from the end of the file, instead of just the one that really exists.)
My code is very simple. The problem is that it produces different results in Linux than in Windows.
with open(path, 'r') as file:
for line in file:
print('>', line, '<', sep='')
'''
# Alternate method:
while True:
line = file.readline()
if not line:
break
print('>', line, '<', sep='')
'''
On Linux (Ubuntu 13.10 to be exact), the final line always ends with a phantom newline. I've tried in both Python 2 and 3. Any idea what's going on here?
Upvotes: 2
Views: 627
Reputation:
What about removing the line break? Is this what you want?
with open('/Users/sebastian/Desktop/Untitled.txt', 'r') as file:
for line in file:
line = line.strip()
if line:
print('>', line, '<', sep='')
prints
>This is the first line<
>this is the second line<
>this is the third line<
PS: You will have problems with your alternate method if there is an empty line somewhere in the middle of your text file.
Have a look at PEP278. The 'U' mode might be helpful in your case
with open('/Users/sebastian/Desktop/Untitled.txt', 'rU') as file:
for line in file:
line = line.strip()
print('>', line, '<', sep='')
prints
>This is the first line<
>this is the second line<
><
>this is the fourth line (blank line above)<
><
And alternatively, you can add an extra line that is platform-specific, e.g., via
import platform
if platform.system()=='Windows':
# do sth
else:
# do sth
Upvotes: 2
Reputation: 11190
Each text file in Linux consists of series of lines plus a terminating new line character. If a file doesn't end with a new line character in linux, it is not considered as a text file. This is defined in the POSIX file system used by linux.
3.206 Line
A sequence of zero or more non- <newline> characters plus a terminating <newline> character.
So it is not the issue with your code. It is just the filesystem under linux. You can simply remove the terminating newline character in the string you read from the file.
Upvotes: 2