[Tutor] TypeError: 'str' object is not callable

Chris Hallman challman at gmail.com
Fri Feb 17 17:08:51 CET 2006


Oh, gee. Do I feel sheepish. I knew I had been staring at the error all
along, but yet couldn't see it.


Thanks!!

On 2/17/06, Kent Johnson <kent37 at tds.net> wrote:
>
> Chris Hallman wrote:
> >
> > Here is my script:
> >         input = file(rpath, "r")
> >         for line in file(rpath):
> <snip>
> > for file in dirList:
> >     filename = file.lower()
>
> You use the name 'file' for a global variable. This hides the builtin
> function 'file'. This is a bit of a gotcha for newbies - the names of
> builtins are not reserverd words, so they can be reasigned to your own
> variables. Some of the names that are commonly (mis)used this way are
> file, list and dict. You just have to learn not to do this.
>
> When you try
>    input = file(rpath, "r")
> file contains a string value, which is not callable the way a function
> is. Hence the error.
>
> The fix is to use a different name for your variable, e.g. 'f'.
>
> Also you are opening the file twice, you don't need the very first line
> above. And 'input' is also the name of a builtin ;)
>
> For reference, here is a list of the built-in functions:
> http://docs.python.org/lib/built-in-funcs.html
>
> > Here is the error:
> >
> >  >pythonw -u "send_file.py"
> > c:\temp\config\rtr0544.txt
> > Exception in thread Thread-1:
> > Traceback (most recent call last):
> >   File "c:\python24\lib\threading.py", line 442, in __bootstrap
> >     self.run()
> >   File "send_file.py", line 45, in run
> >     input = file(rpath, "r")
> > TypeError: 'str' object is not callable
>
> OK, this is saying that you are trying to call a string object, and
> strings can't be called. You call something with the () syntax. So the
> thing you are trying to call is the value of the symbol 'file'.
> Evidently that is a string rather than the built-in file function.
>
> Kent
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060217/2ac19413/attachment.htm 


More information about the Tutor mailing list