[Tutor] Reading opened files

James Reynolds eire1130 at gmail.com
Fri Jun 17 18:44:28 CEST 2011


Your problem is right here:

>>> whole=file.read
>>> print whole

Your re-assigning the method "read()", which is a method of the object
"file" to the variable "whole"

So, when you print "whole" you can see that it is printing the location of
the method in memory. If you were to print file.read you would get the same
results.

Now, to call the method (or a function) you need to add the parentheses with
any arguments that it may need. In your case, you need to do "read()".

This principle holds true with any object.

So, let's say we created a list object by doing this mylist = []

mylist now has all of the methods that a list has. So, for example, I can
append something to the list by going:

mylist.append(1)

printing my list will show a list like this [1]

IDLE 2.6.5
>>> mylist = []
>>> mylist.append(1)
>>> print mylist
[1]

but instead lets say I did this:

>>> b = mylist.append
>>> print b
<built-in method append of list object at 0x0118FD50>

Which can be handy if i needed to do appending all the time, for example:

>>> b(1)
>>> print mylist
[1, 1]

My last piece of advice here is, use the Python documentation in addition to
googling. It's actually very readable (I think anyway)


On Fri, Jun 17, 2011 at 12:20 PM, Lisi <lisi.reisz at gmail.com> wrote:

> Hello :-)
>
> I have got as far as I have,i.e. apparently succeeding in both opening and
> closing two different files, thanks to google, but my struggles to _do_
> something with a file that I have opened are getting me nowhere.  Here is
> my
> latest failure:
>
> >>> file=open("/home/lisi/CHOOSING_SHOES.txt", "r")
> >>> file.close()
> >>> file=open("/home/lisi/CHOOSING_SHOES.txt", "r")
> >>> whole=file.read
> >>> print whole
> <built-in method read of file object at 0xb74c48d8>
> >>> print "%r" % whole
> <built-in method read of file object at 0xb74c48d8>
> >>> print "whole is %r" %whole
> whole is <built-in method read of file object at 0xb74c48d8>
> >>> print "whole is %r" % whole
> whole is <built-in method read of file object at 0xb74c48d8>
> >>>
>
> I'd be extremely grateful if one of you was willing to drop a hint, give me
> some pointers (even e.g. guide me to asking the correct question of
> Google),
> or tell me where I am going wrong.
>
> In general, the author advises leaving any of the extra credit questions
> that
> you are struggling with and coming back to them later.  And in general I
> have
> found that that works.  But this set of extra-credit questions he advises
> mastering before moving on.  And I am stuck on this one at this stage. :-(
> (I think that I have cracked the others.)
>
> Thanks.
>
> Lisi
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110617/61636f5d/attachment.html>


More information about the Tutor mailing list