I need to execute the following command through python. rtl2gds is a tool which reads in 2 parameters: Path to a file and a module name
rtl2gds -rtl=/home/users/name/file.v -rtl_top=module_name -syn
I am reading in the path to the file and module name from the user through argparse as shown 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 read into args.fileread does not get in to the os.system when I call -rtl=args.fileread. Instead, args.fileread itself is being assumed as the file name and the tool flags an error.
I am sure there is a way to read in command line arguments into os.system or some other function (may be subprocess?- but couldnt figure out how). Any help is appreciated.
Don't use os.system()
; subprocess
is definitely the way to go.
Your problem though is that you expect Python to understand that you want to interpolate args.fileread
into a string. As great as Python is, it is not able to 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 a filename to another command, you should not use the FileType
type option! You want a filename, not an open file object:
parser.add_argument('fileread', help='Enter the file path')
But do use 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, the =
is optional and you can use the following call instead, avoiding string concatenation altogether:
subprocess.call(['rtl2gds', '-rtl', args.fileread, '-rtl_top', args.module_name, '-syn'])