3

Here is code from a tutorial in A Byte of Python:

import sys

filename = 'poem.txt'

def readfile(filename):
    #Print a file to standard output
    f = file(filename)
    while True:
        line = f.readline()
        if len(line) == 0:
            break
        print line,
    f.close()

if len(sys.argv) < 2:
    print 'No action specified'
    sys.exit() //<--This is where the error is occurring

if sys.argv[1].startswith('--'):
    option = sys.argv[1][2:] #fetches sys.argv[1] without first 2 char
    if option == 'version':
        print 'Version 1.2'

    elif option == 'help':
        print '''\
This program prints files to the standard output.
Any number of files can be specified.
Options include:
    --version: Prints the version number
    --help: Displays this help'''

    else:
        print 'Unknown option'
    sys.exit()

else:
    for filename in sys.argv[1:]:
        readfile(filename)

When I run this code, this is the error that appears:

Traceback (most recent call last):
  File "C:/Python/sysmodulepr.py", line 17, in <module>
    sys.exit()
SystemExit

I don't understand why. Please help.

dopatraman
  • 13,416
  • 29
  • 90
  • 154
  • You may check [this question](http://stackoverflow.com/questions/1187970/how-to-exit-from-python-without-traceback) for exiting without a traceback – osoner Dec 30 '11 at 17:00
  • I tried running this code, and no "error" appears. The answers below rightly identify this as a `SystemExit` exception, which isn't really an error, but (effectively) a hook that allows you to do cleanup before the program exits. But normally the `SystemExit` exception doesn't produce a traceback like this. There's something weird going on here... – senderle Dec 30 '11 at 17:09
  • 1
    What version of Python you use? This is not a normal Python installation, right? – Niklas B. Dec 30 '11 at 17:10
  • 1
    Tell us more about the context here. How are you executing this? – senderle Dec 30 '11 at 17:11

3 Answers3

3

It's telling you that sys.exit() has executed on line 17 of your program.

The entry for for sys.exit in the Python documentation tells you that this exits your program.

There's no way this line can execute without producing other output, so I think there's something missing in the question.

  • 2
    This is backed by the docs of [SystemExit](http://docs.python.org/library/exceptions.html#exceptions.SystemExit): "When it is not handled, the Python interpreter exits; no stack traceback is printed." – Niklas B. Dec 30 '11 at 17:07
2

If you're using IDLE, it will print the stack anyway. Try running your script from the command line, it won't print that error message when executed outside the IDE.

yurisich
  • 6,991
  • 7
  • 42
  • 63
1

It's not an error. sys.exit() raises SystemExit exception to allow try:... finally block to cleanup used resources

Try in Idle:

import sys

sys.exit()

From documentation for sys.exit():

Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level.

edit

The error shouldn't be normally printed unless you're trying to run the script in some interactive interpreters (for example Idle). It's nothing to worry about, but the script looks like it's standalone, so you should use it as such.

soulcheck
  • 36,297
  • 6
  • 91
  • 90