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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:56:32+00:00 2026-06-17T04:56:32+00:00

I have a script that goes using os.walk to go down a directory structure

  • 0

I have a script that goes using os.walk to go down a directory structure and matching files by extension and copies files to another location.

This is what i have for the file copy:

sourceDir=sys.argv[-2].rstrip("/")
destDir=sys.argv[-1].rstrip("/")
//copy code

So i would just call:

python mycode.py ~/a/ ~/b/ 

What i want to do is add an optional argument switch that will also match by a search pattern:

python mycode.py --match "pattern" ~/a/ ~/b/ 

In my code i would add this extra if:

if "--match" in sys.argvs:
  #try reference the string right after --match"
  for root, dir, files... etc

So to be precise, how can i find “pattern” if –“match” is in sys.argvs?
New to python, so any help would be greatly appreciated.

Thanks!

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

    The argparse library is great at optional argument parsing:

    import argparse
    
    p = argparse.ArgumentParser(description="My great script")
    p.add_argument("sourceDir", type=str, help="source directory")
    p.add_argument("destDir", type=str, help="destination directory")
    p.add_argument("--match", type=str, dest="match", help="search pattern")
    
    args = p.parse_args()
    
    print args.sourceDir, args.destDir, args.match
    

    That way, args.match will be None if it’s not provided:

    Davids-MacBook-Air:BarNone dgrtwo$ python mycode.py ~/a/ ~/b/
    /Users/dgrtwo/a/ /Users/dgrtwo/b/ None
    Davids-MacBook-Air:BarNone dgrtwo$ python mycode.py --match "pattern" ~/a/ ~/b/
    /Users/dgrtwo/a/ /Users/dgrtwo/b/ pattern
    

    It can also tell if there aren’t the right number of arguments:

    usage: mycode.py [-h] [--match MATCH] sourceDir destDir
    mycode.py: error: too few arguments
    

    And includes a help message:

    Davids-MacBook-Air:BarNone dgrtwo$ python mycode.py -h
    usage: mycode.py [-h] [--match MATCH] sourceDir destDir
    
    My great script
    
    positional arguments:
      sourceDir      source directory
      destDir        destination directory
    
    optional arguments:
      -h, --help     show this help message and exit
      --match MATCH  search pattern
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following script that executes all the .reg files in the current directory.
I have a PHP script that imports CSV files and goes through tens of
I have a script that has PHP on it, and a link that goes
I have part of my script that goes like this: while read line do
I have PHP scrip that goes like this: if ($cost_frm < $cost){ echo <script
I have a really confusing problem. I have a PHP script that polls another
I have a script that i use to push files back to my home
Possible Duplicate: Sending mass email using PHP I have a PHP script that sends
I have a bash script that processes some data using inotify-tools to know when
I have a problem with using a script that adds a NiceEdit toolbar 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.