user838437
user838437

Reputation: 1501

Python not reading file, returning empty result

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

Answers (2)

Levon
Levon

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

Makoto
Makoto

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

Related Questions