[Tutor] sys.exit() not working

Andrei Kulakov ak@silmarill.org
Sat, 04 Aug 2001 16:37:20 -0400


On Sat, Aug 04, 2001 at 04:30:02PM -0400, fleet@teachout.org wrote:
> In the following code sys.exit() fails on the first test (denoted
> <---FAILS); but works fine on the second test (denoted <---WORKS).
> 
> The only difference I see is that in the second test I provide an error
> condition and in the first test I don't.  Which brings me to another
> question:  The error returned if there are no jpg/JPG files in the
> directory appears to be a shell error message (RH Linux 7.0) and not a
> Python error (because of popen, I presume).  If I *need* an error
> condition after except, what do I put there (first test)?
> 
> What occurs now, if this is run with no jpg/JPG files in the directory, is
> the error message for test one gets printed, the test one 'sys.exit()'
> gets ignored and the following test gets run.  Entering a "return" there
> causes that test to fail, and the test 2 error message gets printed and
> the file exits to my shell (from whence it was launched).
> 
> TIA,
> 					- fleet -
> 
>  --------------------------------------------
> #! /usr/bin/python
> 
> """
> Creates body of Sylvia's E-bay index.  At this point, file sizing must be completed, captions
> file must be cleaned up and everything ready to go.
> """
> 
> import os
> import sys
> import string
> 
> 
> # Get list of .jpg (or .JPG) files
> try:
>    files=string.split(os.popen("ls -1D *.[jJ][pP][gG]").read(),"\012")
> except:
>    print "ls: *.[jJ][pP][gG]: No such file or directory"

Does this print? If not, then the following line should not and does not
run.

>    sys.exit()          #<--- FAILS
> 
> # Get captions
> try:
>    captions=string.split(open(raw_input("Enter Captions filename: "),"r").read(),"\012")
> except IOError:
>    print "No such file"
>    sys.exit()          #<--- WORKS
> 
> # Compare lengths of files and captions
> if len(files) == len(captions):
>    pass
> else:
>    print "Nr. of files (%s) doesn't match Nr. of captions (%s)" % (len(files), len(captions))
>    sys.exit()
> 
> # Create temp.html for insertion into index.html
> temp=open("temp.html","a")
> 
> # The last line in "files" and "captions" is empty; hence len(files)-1
> # write filenames and captions to temp.html
> for i in range(0,len(files)-1):
>    temp.write('<li><a href=\"%s\">(%s) - %s</a></li>\n' % (files[i], files[i], captions[i]))
> 
> # close temp.html
> temp.close()
> 
> # get index.html from the mirror directories
> os.popen("cp /home/raq2/brickhouse/ebay/index.html /home/dev/brickhouse/ebay")
> 
> # exit
> sys.exit()
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: http://silmarill.org/cymbaline