[Tutor] sys.exit() not working
fleet@teachout.org
fleet@teachout.org
Sat, 4 Aug 2001 16:30:02 -0400 (EDT)
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"
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()