Thursday, 15 July 2010

python - Passing arguments into os.system -



python - Passing arguments into os.system -

i need execute next command through python. rtl2gds tool reads in 2 parameters: path file , module name

rtl2gds -rtl=/home/users/name/file.v -rtl_top=module_name -syn

i reading in path file , module name user through argparse shown below:

parser = argparse.argumentparser(description='read in file..') parser.add_argument('fileread', type=argparse.filetype('r'), help='enter file path') parser.add_argument('-e', help='enter module name', dest='module_name') args = parser.parse_args() os.system("rtl2gds -rtl=args.fileread -rtl_top=args.module_name -syn")

but file path read args.fileread not in os.system when phone call -rtl=args.fileread. instead, args.fileread beingness assumed file name , tool flags error.

i sure there way read in command line arguments os.system or other function (may subprocess?- couldnt figure out how). help appreciated.

don't utilize os.system(); subprocess way go.

your problem though expect python understand want interpolate args.fileread string. great python is, not able read mind that!

use string formatting instead:

os.system("rtl2gds -rtl={args.fileread} -rtl_top={args.module_name} -syn".format(args=args)

if want pass filename command, should not utilize filetype type option! want filename, not open file object:

parser.add_argument('fileread', help='enter file path')

but utilize subprocess.call() instead of os.system():

import subprocess subprocess.call(['rtl2gds', '-rtl=' + args.fileread, '-rtl_top=' + args.module_name, '-syn'])

if rtl2gds implements command line parsing properly, = optional , can utilize next phone call instead, avoiding string concatenation altogether:

subprocess.call(['rtl2gds', '-rtl', args.fileread, '-rtl_top', args.module_name, '-syn'])

python argparse os.system

No comments:

Post a Comment