[Tutor] TypeError when io.open is used

Steven D'Aprano steve at pearwood.info
Mon Jun 28 01:51:50 CEST 2010

On Mon, 28 Jun 2010 08:20:01 am petkovas at dir.bg wrote:

> The full error message is:
> Traceback <most recent call last>:
>     File "insert_into_db_v9.py", line 55, in <module>
>        WHERE testtable_n = %s""", data1,
> str(os.path.splitext(file)[0]))
> TypeError: an integer is required

Unfortunately this isn't helpful, because we're only seeing part of the 
offending line of code. This is because Python tracebacks only show the 
*physical* line which fails, but a logical line of code can be split 
over more than one physical line.

E.g. if I have a module like this:

# test.py
x = 23
y = 23-x
z = (42/

and then import it:

>>> import test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 5, in <module>
ZeroDivisionError: integer division or modulo by zero

it only shows me the last part of the expression (42/y).

So you will need to look at your source code and manually copy and paste 
the rest of the logical line, that is, the line or lines immediately 
before line 55.

> And i would want to add that str() is not the problem. I have tried
> without it and the problem persisted.

The call to str() is totally pointless, since os.path.splitext(file)[0] 
is already a string.

> file = open(
> "C:\\Blender_Library\\BlenderLib\\objectLib\\Faqns\\Osaka2\\faqns_osa
>ka_2.jpg", "rb" )

Windows accepts forwards slashes for pathnames too, so the above can 
more simply be written:


with less chance of error.

> The problem this time was:
> Traceback <most recent call last>:
>     File "insertdb_pg8000.py", line 19, in <module>
>        cursor.execute("UPDATE testtable SET jpeg = %s WHERE
> testtable_n = %s", data1, data2)
>     File "build\bdist.win32\egg\pg8000\dbapi.py", line 243, in _fn
> TypeError: execute() takes at most 3 arguments (4 given)

I'm afraid this is an example of a *slightly* misleading error message 
in Python. the cursor.execute method takes at most three arguments:

# Something like this.
class Cursor:
    def execute(self, a, b):

*but* one of those arguments, self, is automatically filled in by 
Python. So when you call execute, you can only supply TWO arguments:

cursor.execute(a, b)

If you provide three:

cmd = "UPDATE testtable SET jpeg = %s WHERE testtable_n = %s"
cursor.execute(cmd, data1, data2)

then Python fills in self and gives an error message that execute takes 
only three arguments, not four.

So you need to read the documentation for execute to find out what 
arguments it takes, because you can't pass all three of cmd, data1 and 

Steven D'Aprano

More information about the Tutor mailing list