Differences between revisions 3 and 4
Revision 3 as of 2011-12-17 17:24:54
Size: 1110
Editor: host-92-26-97-45
Comment:
Revision 4 as of 2011-12-17 18:41:17
Size: 1993
Editor: PaulBoddie
Comment: Tried to answer the question.
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
= Asking for Help: ... = = Asking for Help: Why when I read a text file python reads it as "<built-in method read of _io.TextIOWrapper object at 0x02954558>" and how do I stop this? =
Line 6: Line 6:
...
Line 22: Line 21:
## Leave the note below so that editors can follow the instructions... ----
Line 24: Line 23:
{{{#!wiki note
When ''answering'' questions, add the CategoryAskingForHelpAnswered category when saving the page. This will move the link to this page from the questions section to the answers section on the [[Asking for Help]] page.
You have to actually call the `read` method as follows:

{{{#!python numbers=disable
a = f.read()
Line 28: Line 29:
The brackets are important because they tell Python to actually ''call'' the method, and this produces the content of the file to be assigned to `a`. If you don't have the brackets, all you are doing is obtaining the `read` method and assigning it to `a`. Thus, when you print `a` you see a piece of text describing the method (which is what `<built-in method read of _io.TextIOWrapper object at 0x02954558>` means) instead of the content of the file.

So the point to remember is this: referring to the method (like with `f.read`) just lets you obtain the method, but if you want to call the method, you have to add brackets. The following also works:

{{{#!python numbers=disable
method = f.read
a = method()
}}}


Here, we split up the part where you get the method from the part where you call the method. There really are two different things going on. -- PaulBoddie <<DateTime(2011-12-17T19:41:14+0100)>>
Line 29: Line 42:
CategoryAskingForHelp CategoryAskingForHelp CategoryAskingForHelpAnswered

Asking for Help: Why when I read a text file python reads it as "<built-in method read of _io.TextIOWrapper object at 0x02954558>" and how do I stop this?

I am writing a program that uses text files to store user information like their name and password. I am using this information to check login info against the info they have provided. This never worked so I decided to do a simple open files program to find the problem.

   1 f = open('test.txt', 'r')
   2 a = f.read
   3 f.close
   4 print(a)

and when I ran it on Idle it printed: <built-in method read of _io.TextIOWrapper object at 0x02954558> Instead of what I had in my text file. Does anyone know why this is happening or how to stop it?


You have to actually call the read method as follows:

a = f.read()

The brackets are important because they tell Python to actually call the method, and this produces the content of the file to be assigned to a. If you don't have the brackets, all you are doing is obtaining the read method and assigning it to a. Thus, when you print a you see a piece of text describing the method (which is what <built-in method read of _io.TextIOWrapper object at 0x02954558> means) instead of the content of the file.

So the point to remember is this: referring to the method (like with f.read) just lets you obtain the method, but if you want to call the method, you have to add brackets. The following also works:

method = f.read
a = method()

Here, we split up the part where you get the method from the part where you call the method. There really are two different things going on. -- PaulBoddie 2011-12-17 18:41:14


CategoryAskingForHelp CategoryAskingForHelpAnswered

Asking for Help/Why when I read a text file python reads it as "<built-in method read of _io.TextIOWrapper object at 0x02954558>" and how do I stop this? (last edited 2011-12-17 18:41:17 by PaulBoddie)

Unable to edit the page? See the FrontPage for instructions.