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

  • SEARCH
  • Home
  • 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 8708775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:12:59+00:00 2026-06-13T04:12:59+00:00

I am reading python tutorial here and there is one thing that I can’t

  • 0

I am reading python tutorial here and there is one thing that I can’t quite make sense out of. From the docs:

for arg in sys.argv[1:]:
    try:
        f = open(arg, 'r')
    except IOError:
        print 'cannot open', arg
    else:
        print arg, 'has', len(f.readlines()), 'lines'
        f.close()

From the explanation in the doc:

The use of the else clause is better than adding additional code to the try clause because it avoids accidentally catching an exception that wasn’t raised by the code being protected by the try … except statement.

My question is it how does it protect against accidentally catching an exception that wasn’t raised in this try block. I don’t see what they mean. Can anyone give an example?

  • 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-13T04:13:00+00:00Added an answer on June 13, 2026 at 4:13 am

    One possible alternative approach that you might try is this:

    for arg in sys.argv[1:]:
        try:
            f = open(arg, 'r')
            print arg, 'has', len(f.readlines()), 'lines'
            f.close()
        except IOError:
            print 'cannot open', arg
    

    Here if there is an exception during f.readlines() or f.close() then it will be caught by the except block and you will get the error message 'cannot open' which is wrong.

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

Sidebar

Related Questions

Reading the Python 3.2 tutorial here , towards the end one of the examples
I remember coming reading about a python module/lib that helped make nice command line
I started learning python today from the tutorial on the official site. When reading
I've been reading about Python's urllib2's ability to open and read directories that are
I am reading the book Think Python by Allen Downey. For chapter 4, one
I am reading cpython code for python 3k and I have noticed, that __missing__
I'm reading Tutorial on Network Programming with Python , and in this document the
In the book that I am reading on Python, it keeps using the code
Possible Duplicate: How to conduct buffer overflow in PHP/Python? I was reading this tutorial
can someone help with me reading # char in python? i can't seem to

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.