Exit gracefully if file doesn't exist
Asked Answered
N

2

27

I have following script in Python 3.2.3:

try:
    file = open('file.txt', 'r')
except IOError:
    print('There was an error opening the file!')
    sys.exit()

#more code that is relevant only if the file exists

How do I exit gracefully, if the file doesn't exist (or there is simply an error opening it)?

I can use exit(), but that opens a dialog panel asking if I want to kill the application.

I can use sys.exit(), but that raises a SystemExit exception which doesn't looks great in output. I get

Traceback (most recent call last):   
File "file", line 19, in <module>
    sys.exit() SystemExit

I can use os.exit(), but that kills the Python on C level, without any cleanups I might be performing.

I can use a boolean variable and wrap all subsequent code in if... but that is ugly, and this is not the only check I am performing. So I would have like six nested ifs...

I just want to print the 'There was an error...' and exit. I am working in IDLE.

Nunatak answered 16/11, 2012 at 21:49 Comment(8)
This looks like the correct way to do it. Generally, you don't see the SystemExit exception (unless you're catching it someplace else which you probably shouldn't be) ... I'm not sure what you're talking about it looking good in the output.Secund
You might be able to return from the current function.Lenwood
Raise an exception of your own that gets caught at top-level, or caught and rethrown where you need cleanup. (That said, you shouldn't really need much explicit cleanup that unwinding the stack won't do for you.)Loria
Better, call sys.exit() and catch the SystemExit at the top level. (I don't see SystemExit myself; what environment are you running in?)Heeheebiejeebies
@RussellBorogoveI am using IDLE.Nunatak
have you tried calling sys.exit(0)?Systole
@COpython yes, it still prints the SystemExitNunatak
@JakubZaverka - Just run your script outside of IDLE then and you can see that sys.exit won't display SystemExit. IDLE does so that you know if the program finished or exited abnormally.Balalaika
P
21

This is a very graceful way to do it. The SystemExit traceback won't be printed outside of IDLE. Optionally you can use sys.exit(1) to indicate to the shell that the script terminated with an error.

Alternatively you could do this in your "main" function and use return to terminate the application:

def main():
    try:
        file = open('file.txt', 'r')
    except IOError:
        print('There was an error opening the file!')
        return

    # More code...

if __name__ == '__main__':
    main()

Here the main execution code of the application is encapsulated in a single function called "main", then executed only if the script is executed directly by the Python interpreter, or in other words, if the script it not being imported by another script. (The __name__ variable is set to "__main__" if the script is being executed directly from the command line. Otherwise it will be set to the name of the module.)

This has the advantage of gathering all script execution logic into a single function, making your script cleaner, and enables you to cleanly exit your script using the return statement, just like in most compiled languages.

Puebla answered 16/11, 2012 at 22:20 Comment(3)
I ended up using the main function as you suggested. I however doesn't understand the purpose of the if before calling main.Nunatak
@JakubZaverka: I've updated the answer with some information about the "ifmain" method.Puebla
IOError is an alias of OSError in Python 3Underfur
F
2

Using sys.exit() is fine. If you're that concerned with output, you can always add an extra try/except block within your error handling section to catch the SystemExit and stop it from being directed to console output.

try:
    file = open('file.txt', 'r')
except IOError:
    try:
        print('There was an error opening the file!')
        sys.exit()
    except SystemExit:
        #some code here that won't impact on anything
Frankfort answered 11/4, 2013 at 8:55 Comment(1)
This solution does not work as the except SystemExit: statement will ignore the the sys.exit() call thus desired exit.Frond

© 2022 - 2024 — McMap. All rights reserved.