Skip to content Skip to sidebar Skip to footer

Python Quit Function Not Working

I am using the following check in one of my scripts: if os.path.exists(FolderPath) == False: print FolderPath, 'Path does not exist, ending script.' quit() if os.path.isfil

Solution 1:

Per the documentation, quit() (like other functions added by the site module) is intended only for interactive use.

Thus, the solution is twofold:

  • Check whether os.path.exists(os.path.join(FolderPath, GILTS)), not just os.path.exists(FolderPath), to ensure that the code attempting to exit the interpreter is actually reached.

  • Use sys.exit(1) (after import sys in your module header, of course) to halt the interpreter with an exit status indicating an error from a script.

That said, you might consider just using exception handling:

from __future__ import print_function

path = os.path.join(FolderPath, GILTS)
try:
    df_gilts = pd.read_csv(path)
except IOError:
    print('I/O error reading CSV at %s' % (path,), file=sys.stderr)
    sys.exit(1)

Post a Comment for "Python Quit Function Not Working"