Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

For instance you could do something like this:

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

print data # display data read 

Using with will also close the file for your automatically

Solution 2:

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

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

Post a Comment for "Python Not Reading File, Returning Empty Result"