posix.error is a tuple, not an object or a string as docs say

Ivan Van Laningham ivanlan at callware.com
Tue Aug 10 16:23:03 EDT 1999


Hi Ben, All--

Ben Gertzfield wrote:
> 
> >>>>> "Michael" == Michael P Reilly <arcege at shore.net> writes:
> 
>     Michael> The long and short of this is that you can use it like a
>     Michael> tuple if more than one argument was given, and as the
>     Michael> object otherwise.
> 
>     Michael> For some of your own reassurance, print the type of
>     Michael> "error" in the except clause.
> 
> Right, but my problem is that I can't use it as an object as the
> documentation says..
> 
> Python 1.5.1 (#1, Dec 17 1998, 20:58:15)  [GCC 2.7.2.3] on linux2
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> import os
> >>> try:
> ...     input = os.fdopen(42)
> ... except os.error, error:
> ...     print error.errno
> ...     print error.strerror
> ...
> Traceback (innermost last):
>   File "<stdin>", line 4, in ?
> AttributeError: errno
> 
> Yet the documentation says:
> 
> "When exceptions are classes, this exception carries two attributes,
> errno and strerror. The first holds the value of the C errno variable,
> and the latter holds the corresponding error message from strerror()."
> 
> So something else is a bit wacky, no?
> 

Try this script on a nonexistent file:  "python readit.py
doesntexist.txt", for example.
---------------------------------------
#!/usr/local/bin/python

import os
import sys
import errno

if __name__ == "__main__" :
    if len ( sys.argv ) > 1 :
	try:
	    f = open ( sys.argv[ 1 ], "rb" )
	except :
	    x = sys.exc_info()
	    print type (x)
	    print type(x[0])
	    print dir(x[0])
	    print type(x[1])
	    print dir(x[1])
	    print x[1].errno
	    print type(x[2])
	    print "No file named %s!" % ( sys.argv[ 1 ] )
	    sys.exit(1)
	while 1 :
	    t = f.readline ( )
	    if t == '' :
		break
	    if '\n' in t :
		t = t[ : -1 ]
	    if '\r' in t :
		t = t[ : -1 ]
	    sys.stdout.write ( t + '\n' )
	f.close ( )
------------------------------------------------


> --
> Brought to you by the letters C and H and the number 11.

<brought-to-you-by-chapter-8-of-teach-yourself-python-in-24-hours>-ly
y'rs,
Ivan;-)
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan at callware.com
ivanlan at home.com
http://www.pauahtun.org
See also: 
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps:  Cu Chi, Class of '70
----------------------------------------------




More information about the Python-list mailing list