Newbie: opening files.

Fredrik Lundh fredrik at pythonware.com
Fri May 19 15:44:11 CEST 2000


Bart Holthuijsen wrote:
> The following error message appears:
> 
> C:\Stage\xalan>python open.py
> Traceback (innermost last):
>   File "open.py", line 25, in ?
>     if __name__=='__main__': main()
>   File "open.py", line 13, in main
>     f = open('Yearend.xml','r')
> TypeError: illegal argument type for built-in operation
> 
> What am I doing wrong?

from-import is a dangerous tool.  don't use it unless you
know exactly what you're doing ;-)

    >>> print open.__doc__
    open(filename[, mode[, buffering]]) -> file object

    Open a file.  The mode can be 'r', 'w' or 'a' for reading (default),
    writing or appending.  The file will be created if it doesn't exist
    when opened for writing or appending; it will be truncated when
    opened for writing.  Add a 'b' to the mode for binary files.
    Add a '+' to the mode to allow simultaneous reading and writing.
    If the buffering argument is given, 0 means unbuffered, 1 means line
    buffered, and larger numbers specify the buffer size.

    >>> from sys import *
    >>> print open.__doc__
    open(filename[, mode[, buffering]]) -> file object

    Open a file.  The mode can be 'r', 'w' or 'a' for reading (default),
    ...

    >>> from os import *
    >>> print open.__doc__
    open(filename, flag [, mode=0777]) -> fd
    Open a file (for low level IO).

(in other words, there's another 'open' function in the os
module, and since you import *everything* from that module
into your own namespace, you can no longer access the
original builtin...)

for more info on from-import (and when it's okay to use it), see:
http://www.pythonware.com/people/fredrik/fyi/fyi06.htm

</F>





More information about the Python-list mailing list