Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

python - Passing arguments into os.system

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...