0

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().

3

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.