What the heck? open() requires an integer?

Alex Martelli aleax at aleax.it
Fri Sep 20 11:20:57 CEST 2002


Fausto Arinos de A. Barbuto wrote:

>     The open() function is returning me a strange error.
>     An attempt to create an output file with
> 
>             f = open("output.txt","w")
> 
>     yields the following error:
> 
>     Traceback (most recent call last):
>     File "<interactive input>", line 1, in ?
>     TypeError: an integer is required
> 
>     That happens when I am using either Pythonwin or
>     IDLE 0.8, but not in interactive mode. In this latter
>     case I can open, write to and close a file OK.
> 
>     What's the matter? Thanks in advance!

I'd be willing to bet that you did what you SHOULDN'T do,
specifically a statement
        from somewhere import *
before this point in your interactive session.  I'd lay
good odds that the 'somewhere' is os.

Don't do it.  "from ... import *" tramples all over your
namespace: after you use it, how can you know WHAT
function object is referred to by any given name?

Well, if you "print open" you could be able to tell, at
least in Python 2.2...:

>>> print open
<type 'file'>
>>> from os import *
>>> print open
<built-in function open>
>>>

So, when you're calling open after "from os import *",
you're really calling os.open -- and THAT one does indeed
require an integer (as its second argument).


A suggestion: forget all about the existence of statement
"from" until you feel you have totally mastered Python.  I
think you'll have an easier time learning, using, and
mastering Python, if you ALWAYS use the import statement
when you need to do imports, rather than using from.  (The
"from ... import *" is the main issue, but there are others:
e.g., built-in reload won't do what you expect if you've
used statement from -- only if you've used statement import).


Alex




More information about the Python-list mailing list