[Tutor] sys.exit() not working
Bruce Sass
bsass@freenet.edmonton.ab.ca
Sat, 4 Aug 2001 14:44:27 -0600 (MDT)
On Sat, 4 Aug 2001 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).
<...>
> # 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
You need to test if "files" is really a list of files, or a message
from "ls", Python has no way of knowing the later is really an error.
- Bruce