1

aaa.txt:

aszczx
d
as
w
ad

python script:

f = open('aaa.txt','r')
f.read()
f.close()

Console:

C:\Python27>test.py

C:\Python27>

Why is it not displaying the contents of the file?

Thanks,

2 Answers 2

6

You are not displaying the contents of the file, just reading it.

For instance you could do something like this:

with open('aaa.txt') as infp:
    data = infp.read()

print data # display data read 

Using with will also close the file for your automatically

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

2 Comments

ah great, not sure how I missed that. Thanks!
Note that the asker used read() while you used readlines() - the two are different. readlines() will return a list of strings, while read() will return a big string.
1

You can save the lines you read in to a variable, then print it later.

lines = f.read()
# Later...
print lines

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.