What the heck? open() requires an integer?

Fausto Arinos de A. Barbuto fbarbuto at telusplanet.net
Fri Sep 20 13:27:08 EDT 2002

Hi Alex and Henrik,

    Thanks so much for your help. I thought it might be
    that open() had been superseded by some other
    function having the same name (but different syntax),
    but didn't know which one.

    Also, it is a good idea to learn more Python before
    venturing to develop code. Or to adapt someone
    else's source code, which was exactly the case.


Alex Martelli <aleax at aleax.it> wrote:

> 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