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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:39:22+00:00 2026-06-04T02:39:22+00:00

I have several different input files, saved with case numbers .1, .2, etc to

  • 0

I have several different input files, saved with case numbers “.1”, “.2”, etc to differentiate them. I am building a script that operates on these files, and I would like to use argparse to allow the user to specify the particular case number to operate on, or use “_” to specify the last saved case (i.e. the input file with the largest case number). Something like;

> ls 
   file.1, file.2, file.3
> my_script.py 2
   (operates on file.2)
> my_script.py _
   (operate on file.3)

Is there a way I can specify “any integer” as one choice and “_” as the second choice? Something like;

parser = argparse.ArgumentParser()
parser.add_argument('case', choices=[anyint, '_'])
  • 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-04T02:39:24+00:00Added an answer on June 4, 2026 at 2:39 am

    You could use the type argument to add_argument(...) instead. For example:

    import os
    import argparse
    
    def intOrUnderscore(s):
        if s != '_':
            return int(s)
        cases = (n for n in os.listdir(".") if n.startswith("file."))
        return max(int(c[c.rindex(".")+1:]) for c in cases)
    
    parser = argparse.ArgumentParser()
    parser.add_argument('case', type=intOrUnderscore)
    
    args = parser.parse_args()
    print args.case
    

    When I run this I get:

    $ ls
    file.1  file.2  file.3  s.py
    $ python s.py 2
    2
    $ python s.py _
    3
    

    Alternately, you could build the choices list in code:

    import os
    import argparse
    
    cases = [n[n.rindex(".")+1:] for n in os.listdir(".") if n.startswith("file.")]
    cases.append("_")
    parser = argparse.ArgumentParser()
    parser.add_argument('case', choices = cases)
    
    args = parser.parse_args()
    print args.case
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several different content type nodes (videos, image galleries, stories...) that I would
Total beginner question about jQuery: I have a Form that contains several TextBoxes (input
I have a Python script that needs to process a large number of files.
I have several different lists I want to call. They all have the same
I have several sites in different domains: example.com , example.org , mail.example.com and passport.example.org
In our infrastructure we have several nodes with different capabilities (in terms of services).
I have a large class, which I have divided into several different class extension
I have several entities which respresent different types of users who need to be
I have an app with several different tabs. The first of these tabs should
I have several similar user controls which display listviews of respectively different data entities.

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.