Newbie: opening files.

Cameron Laird claird at starbase.neosoft.com
Sat May 20 01:52:35 EDT 2000


In article <002f01bfc198$52d7d710$0500a8c0 at secret.pythonware.com>,
Fredrik Lundh <fredrik at pythonware.com> wrote:
>Bart Holthuijsen wrote:
>> The following error message appears:
>>=20
>> C:\Stage\xalan>python open.py
>> Traceback (innermost last):
>>   File "open.py", line 25, in ?
>>     if __name__=3D=3D'__main__': main()
>>   File "open.py", line 13, in main
>>     f =3D open('Yearend.xml','r')
>> TypeError: illegal argument type for built-in operation
>>=20
>> 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=3D0777]) -> 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
			.
			.
			.
Good reference.

Fredrik, the claim that "you can no longer access
the original builtin" is slightly ambiguous.  Mr.
Holthuijsen might also want to know that he can
write
  from os import *
  import __builtin__
  
  def main():
   f = __builtin__.open('c:\stage\Xalan\Yearend.xml', 'r')
   print "file opened"
  
  main()
with happier results than he was first receiving.
Thanks, by the way, to Andrew Kuchling, who
straightened me out on access of the __builtin__
namespace.

Here's the part I don't understand:  why does David
B.'s book show on page 147 a signature for os.open
of (file [, flags [, mode]])?  Was there ever a time
when 'flags' was optional?
-- 

Cameron Laird <claird at NeoSoft.com>
Business:  http://www.Phaseit.net
Personal:  http://starbase.neosoft.com/~claird/home.html



More information about the Python-list mailing list