Reputation: 413
from sys import argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
When I run this code, using the name of a text file as the argument 'ex15_sample.txt' then it returns what is inside the text document.
But when I change the last line to:
print txt
Then it displays this:
<open file 'ex15_sample.txt', mode 'r' at 0x004A6230>
I'm not really sure what the difference is since the txt
variable should be opening the file. I understand that the read
command reads the file but in the docs it says the open
one returns a file object and I'm not sure what this means.
Upvotes: 1
Views: 293
Reputation: 29794
open()
function will return a file
object which representation is indeed: <open file 'ex15_sample.txt', mode 'r' at 0x004A6230>
.
In order the get the file's contents you need to read()
it. That's why when you print txt.read()
you get what you expect.
Upvotes: 5