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 7933657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:23:54+00:00 2026-06-03T21:23:54+00:00

Good day! i have a code below: def initial(*args): for arg in args: with

  • 0

Good day!
i have a code below:

def initial(*args):
    for arg in args:
        with open(arg) as f:
            print 'passed'

it called from this code:

if __name__ == '__main__':
   initial('test_staff/1.txt', 'test_staff/d2.txt', 'test_staff/1.txt')

My question is, if second parametr ‘test_staff/d2.txt’ broken(file not exist), how to continue execute a function(with third param)?

I see several methods to do it:

  1. write function to pass existance
  2. use try, throw, finally.

but how it perform with “with statement from pep” ?

Thank you!

upd: function name changed from __initial__() to initial()

  • 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-03T21:23:55+00:00Added an answer on June 3, 2026 at 9:23 pm

    I’d suggest to check whether the file exists or doing some try / except

    import os
    
    def main(*args):
        for arg in args:
            if not os.path.exists(arg):
                continue
    
            with open(arg) as f:
                print("passed")
    

    Otherwise you could just catch the exception (IOError)

    def main(*args):
        for arg in args:
            try:
                with open(arg) as f:
                    print("passed")
            except IOError:
                print("File doesn't exist")
    

    Some people say “Try and Catch” instead of doing many checks, there are others that prefer checking instead of trying and catching! IMHO, both of them are fine!

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

Sidebar

Related Questions

Hello and good day to all. I have the following code to retrieve from
Good day! I right now have a function the drags an element from a
Good day code knights, I have a tricky problem that I cannot see a
Good day. I have a dropdown list control called audioList. Whenever the page loads
Good day, I have weird problem with drawing image. Code and pictures say more
Good day to all. I have a problem with the following code: textBox.addEventListener(KeyboardEvent.KEY_DOWN,handler); function
Good day! I'm practising materials from Ruby on Rails Tutorial by Michael Hartle. Below
Good day, I have this problem with Html.DropDownListFor which I can't seem to work
Good day, could you please help me look and this code and tell me
Good day I wanted to have a general URL to access the Desktop of

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.