Newbie: opening files.

Thomas Wouters thomas at xs4all.net
Fri May 19 15:31:53 CEST 2000


On Fri, May 19, 2000 at 03:22:28PM +0200, Bart Holthuijsen wrote:

> now this is what I try to do:

> -------------------------
> from sys import *
> from os import *
> from string import *
> 
> def main():
>  chdir('c:\stage\Xalan')
>  f = open('Yearend.xml','r')
>  print "file opened"
> -------------------------

> 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?

This:
> from sys import *
> from os import *
> from string import *

os.open() overwrites the builtin open(), and os.open() takes different
arguments. A friendly tip: do *not* use 'from foo import *' until you
realize what it does -- and then do not use it either :-) If you really
dislike the extra few characters it takes to prepend 'os.' or 'sys.', import
the symbols you need by hand:

from os import system, fork
from sys import argv, exit
from string import letters, replace

But I think you'll find in larger programs, just importing the modules and
using the 'full' name makes your code a lot more obvious. Especially when
debugging.. you wont have to re-trace to find out what a particular variable
is bound to :)


-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list