[Tutor] Two sys.exit questions

Luke Paireepinart rabidpoobear at gmail.com
Wed Feb 28 21:11:51 CET 2007

> (Although, I'm not sure what you meant by "working" in the below case, 
> since your example doesn't exit the interpreter.)
> [snip]
>     try:
>        f = file('somefile_you_have.txt','r')
>        sys.exit(0)
>     except IOError:
>        print "You had an error on file input"
It would if you had a file named 'somefile_you_have.txt' in the same 
working directory as your python interpreter is set to.
If you don't, the f = file( ... ) line will fail to find the file to 
open for input, so it will throw an IOError and print out
"You had an error on file input"
If you do have such a file, the assignment to f will pass successfully, 
sys.exit will throw a SystemExit, and the except block won't catch it 
because it only catches IOErrors.


P.S. Please use the reply-all button so your replies go to the list and 
not to me.

More information about the Tutor mailing list