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

Kent Johnson kent37 at tds.net
Fri Feb 17 16:42:56 CET 2006


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



More information about the Tutor mailing list