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!

user1957413
  • 105
  • 1
  • 12

3 Answers3

1

You can use module OptionParser. example:

from optparse import OptionParser
usage = 'python test.py -m'
parse = OptionParser(usage)
parse.add_option('-m', '--match', dest='match', type='string'
                 default='', action='store',
                 help='balabala')
options, args = parse.parse_args()

Update: If you are using python2.7, argparse will be better. The usage is similar as OptionParser.

jinghli
  • 617
  • 4
  • 11
0

Please don't try and parse the string yourself. Use the argparse module instead.

Tim
  • 11,710
  • 4
  • 42
  • 43
0

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
David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • Thanks for the help. Lets say the --match is provided, how can i use the string patter in a loop? More precisely i am trying to do: if not pattern in file: continue. – user1957413 Jan 09 '13 at 02:37
  • What kind of pattern? Do you want it to be a regular expression or just allow wildcards (like `*`)? (If it's exact string matching, then your pseudocode of `if not pattern in file: continue` already works!) – David Robinson Jan 09 '13 at 02:46
  • It would be, for simplicity, just exact matching ( sorta like String.Contains();). I don't access to my linux machine to try it out, but what you mean by the pseudo code already works? My confusion is that i am thinking for pattern as a variable that was set somewhere, it could be anything. So basically if i ran: `python myscript.py ~/a/ ~/b/ --match "foo"`, if the string foo is not found in the filelist returned by os.walk, continue. How can i reference foo in the `if not statement`? EDIT: I think i see it, it would be `if not args.match continue:`, right? – user1957413 Jan 09 '13 at 03:50
  • @user1957413: It would be `if not args.match in file: continue` – David Robinson Jan 09 '13 at 04:32
  • Yup, that's what i thought. Thanks! – user1957413 Jan 09 '13 at 04:34