Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8843161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:09:02+00:00 2026-06-14T11:09:02+00:00

I have following script in Python 3.2.3: try: file = open(‘file.txt’, ‘r’) except IOError:

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:09:03+00:00Added an answer on June 14, 2026 at 11:09 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following in my python script.. hostIP = sys.argv[1] hostsFileLoc = hosts.txt
I have the following script below where I try to mimic a file upload
I have the following script #!/bin/sh if [cat stream_should_be_running.txt == 'true']; then #file will
I have following code in a python script try: # send the query request
I have the following python script which takes some inputs and puts them in
I have the following script for sending mails using python import smtplib from email.mime.multipart
I have the following simple python test script that uses Suds to call a
i have something similar to the following... used a python script to JSON to
I have the following script running successfully. However if I try to use a
I have a python script that will take each file in a directory, iterate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.