Run Unix shell command $ parse command line arguments in python

hue prasad413in at gmail.com
Fri Apr 22 06:16:02 EDT 2005


Thanks for your reply

I started writing the script.. I have gone through documentation for
getopt

import string, getopt, sys

def usage():

    print '''myscript.py -- uses getopt to recognize options
Options: -n  -- No
         -t  -- T
         -h  -- help
         -i  -- i
         -o  -- Output:filename'''
    sys.exit(1)
def main():

    print "SYS ARGV: ", ",".join(sys.argv)

# Define the Options

Options = {
        'n:': 'Number=',
        't:': 'T',
        'h' : 'help',
        'i' : 'i',
        'o' : 'Output_file',
    }
shortOpts = ''.join(Options.keys())
longOpts  = Options.values()


try:
        (opts, args) = getopt.getopt(argv[1:], shortOpts, longOpts)
except getopt.error, msg:
        print "Unrecognized argument or option"
    # end try


for (opt, arg) in opts:

        if opt in ('-n', '--Number'):
            print '-n is the Number', Number
            sys.exit()

        elif opt in ('-t', '--T'):
            print '-t is the T', T
            sys.exit()

        elif opt in ('-h', '--help'):
            usage()
            print " "
            sys.exit()

        elif opt in ('-i', '--i'):
            print " I", i


        elif opt  in ('-o', '--Output Filename'):
            print "Output", Output


        # end if
       # end for

            print "OPTS: ", ",".join([repr(o) for o in opts])
        print "ARGS: ", ",".join(args)

if __name__ == "__main__":
    main()

with the above code, I am planning to do command line parsing. But how
to run unix shell command? DO i have to use os Module/  import command?

How should i proceed further, to
 import commands
 commands.getstatusoutput('ls /bin/ls')

Please suggest me some ideas how to proceed further

Thanks




More information about the Python-list mailing list