Reputation: 1501
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,
Upvotes: 1
Views: 15092
Reputation: 143047
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
Upvotes: 6
Reputation: 106390
You can save the lines you read in to a variable, then print it later.
lines = f.read()
# Later...
print lines
Upvotes: 1