0

I am trying to open a file but it is displaying nothing.

openf = open('C:\Python27\NEWS', 'r')
openf.read()

It is neither displaying text nor any error. What could be the reason?

and when i write like this

openf = open('C:\Users\K\Desktop\wait.txt', 'r')
>>> print openf

This gives Output:

<open file 'C:\\Users\\K\\Desktop\\wai.txt', mode 'r' at 0x0000000002B4DDB0>

What does this mean?

1 Answer 1

1

read doesn't display anything - it merely returns a string. If you're not at an interactive prompt, the only output you will see is what you print.

openf = open('C:\Python27\NEWS', 'r')
print openf.read()

print openf will give details about the openf object. It's an open file pointed at that file name, it was opened in "read" mode, and it exists in memory at address 0x0000000002B4DDB0.

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

5 Comments

then how to display the contents of file
It is not displaying anything, though my file has content
Works on my machine. Are you doing anything else in your script that you haven't mentioned here?
it worked now.. I have one more doubt, What type of files we can read?
Any kind of file. The extension makes no difference.

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.