Reputation: 197
Why read()
returns an empty string when it reaches the end of the file; this empty string shows up as a blank line. I know we can remove it using rstrip()
.
Upvotes: 0
Views: 2369
Reputation: 172
I have had this problem for a while and, I have been looking for the answer to this question. I recently found the solution.
You must set the .read()
to a string
This will work :
read_file = open("file.txt", "r")
file_string = read_file.read()
print(file_string)
read_file.close()
I Hope this helps. It worked for me, so I am confident it will work for you.
Upvotes: 0
Reputation: 17368
read() method returns empty string because you have reach end of the file and there is no more text in the file.
f = open('f.txt')
print f.read()
print f.tell()
Here f.tell() will give you the seek position and when you do f.tell() it would be at the end of file and returns the length of the file.
Upvotes: 1