[Tutor] Newbie Question:Regarding Command line Parsing and Run Unix
Shell Command
Prasad Kotipalli
prasad413in at gmail.com
Fri Apr 22 22:09:53 CEST 2005
Hello evryone
I am a newbie to python. I have a makefile which i can compile in
UNIX/LINUX, But i
I am planning to write a python script which actually does what my
MAKEFILE does. The make file is
#Makefile and some scripts to give output
#numbers
#Change till sign #END
var1:=564-574
a1 = 23
b1 = 678
kxm = ixm_7.gh
out = c$(var1)_s.gh
imageim
#END
#other files
file1 = sec$(b1).jk
file2 = sgf$(a1)
file3 = p$(b1).gh
prg3scriptfile = prg3_produce.xx
findoutdir = Dir:~/home/
x=67
#work
evrything: prg1 script2 prg3 script4
withoutprg1: script2 prg3 script4
prg1:
s$(ca)$(d) .
script2: Makefile
./script2 -i '$(file2)' -o '$(file1)' -a $(x) -n
prg3:
: > $(prg3scriptfile)
>> $(prg3scriptfile)
prg3 $(prg3scriptfile)
rm $(prg3scriptfile)
script4:
./script4 $(kxm) $(file2) $(out) $(var1)
I want to write a python script to replace this Makefile.
I Have tried using getopt/optparse for parsing command line options
How to Run Unix shell command from python.
My Task is
Myprg.py a1 b1 kxm out
Kindly suggest me some ideas/comments.
I started writing the code
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
koid wayne
More information about the Tutor
mailing list