Passing arguments into os.system

泪湿孤枕 提交于 2019-11-28 04:31:18

问题


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.


回答1:


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'])


来源:https://stackoverflow.com/questions/14892355/passing-arguments-into-os-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!