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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:36:09+00:00 2026-05-20T17:36:09+00:00

I would like to combine the functionality of two arguments to one. Currently -f

  • 0

I would like to combine the functionality of two arguments to one. Currently -f may be used to specify a single file or wild-card, -d may specify a directory. I would like -f to handle its currently functionality or a directory.

Here’s the current options statements:

parser.add_option('-d', '--directory',
        action='store', dest='directory',
        default=None, help='specify directory')
parser.add_option('-f', '--file',
        action='store', dest='filename',
        default=None, help='specify file or wildcard')
if len(sys.argv) == 1:
    parser.print_help()
    sys.exit()
(options, args) = parser.parse_args()

Here is the logic in the functionality, can these be combined?

filenames_or_wildcards = []

# remove next line if you do not want allow to run the script without the -f -d
# option, but with arguments
filenames_or_wildcards = args # take all filenames passed in the command line

# if -f was specified add them (in current working directory)
if options.filename is not None:
    filenames_or_wildcards.append(options.filename)

# if -d was specified or nothing at all add files from dir
if options.directory is not None:
    filenames_or_wildcards.append( os.path.join(options.directory, "*") )

# Now expand all wildcards
# glob.glob transforms a filename or a wildcard in a list of all matches
# a non existing filename will be 'dropped'
all_files = []
for filename_or_wildcard in filenames_or_wildcards:
    all_files.extend( glob.glob(filename_or_wildcard) )
  • 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-05-20T17:36:10+00:00Added an answer on May 20, 2026 at 5:36 pm

    You can pass a list of wildcards, directories and files to this function. However, your shell will expand your wildcards if you do not put them between quotes in the command line.

    import os
    import glob
    
    def combine(arguments):
        all_files = []
        for arg in arguments:
            if '*' in arg or '?' in arg:
                # contains a wildcard character
                all_files.extend(glob.glob(arg))
            elif os.path.isdir(arg):
                # is a dictionary
                all_files.extend(glob.glob(os.path.join(arg, '*')))
            elif os.path.exists(arg):
                # is a file
                all_files.append(arg)
            else:
                # invalid?
                print '%s invalid' % arg
        return all_files
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two views that I would like to combine into one. The first
I have two regualar rexpressions I would like to combine into the one for
In javascript I would like to combine two objects into one. I got {
I would like to combine these four queries into one: SELECT COUNT(ID) FROM conversations
I have these 2 queries that I would like to combine into one big
I would like to combine bitmaps to one texture and apply it to a
I would like to combine setter/getter in one method, in C++, in order to
I have multiple datasets that I would like to combine into one. There is
I would like to combine many commits in to one. I have followed the
I have 2 queries that I would like to combine into one. Basically I

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.