Passing arguments to os.system

I need to execute the following command through python. rtl2gds is a tool that reads 2 parameters: file path and module name

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

      

I am reading the file and module name path from user via argparse as below:

parser = argparse.ArgumentParser(description='Read in a file..')    
parser.add_argument('fileread', type=argparse.FileType('r'), help='Enter the file path')    
parser.add_argument('-e', help='Enter the module name', dest='module_name')    
args = parser.parse_args()    
os.system("rtl2gds -rtl=args.fileread -rtl_top=args.module_name -syn")

      

But the file path that is being read into args.fileread does not end up in os.system when I call -rtl = args.fileread. Instead, args.fileread takes the filename itself and the tool throws an error.

I'm sure there is a way to read the command line arguments in os.system or some other function (could be a subprocess? - but couldn't figure out how to do it). Any help is appreciated.

+3


source to share


1 answer


Do not use os.system()

; subprocess

- this is definitely the way to go.

Your problem is that you expect Python to understand what you want to interpolate args.fileread

into a string. As good as Python, it can't read your mind like that!

Use string formatting instead:

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

      

If you want to pass the filename to another command, you should not use the type parameter FileType

! You want the filename, not the open file:



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

      

But use subprocess.call()

instead os.system()

:

import subprocess

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

      

If it rtl2gds

parses the command line correctly, it =

is optional and you can use the following call instead, avoiding string concatenation:

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

      

+9


source







All Articles