Python 将参数传递给 os.system

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14892355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:46:40  来源:igfitidea点击:

Passing arguments into os.system

pythonargparseos.system

提问by Nanditha

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

我需要通过python执行以下命令。rtl2gds 是一个读取 2 个参数的工具:文件路径和模块名称

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:

我正在通过 argparse 从用户读取文件和模块名称的路径,如下所示:

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.

但是当我调用 -rtl=args.fileread 时,读入 args.fileread 的文件路径不会进入 os.system。相反, args.fileread 本身被假定为文件名,并且该工具会标记错误。

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.

我确信有一种方法可以将命令行参数读入 os.system 或其他一些函数(可能是子进程? - 但无法弄清楚如何)。任何帮助表示赞赏。

采纳答案by Martijn Pieters

Don't use os.system(); subprocessis definitely the way to go.

不要使用os.system(); subprocess绝对是要走的路。

Your problem though is that you expect Python to understand that you want to interpolate args.filereadinto a string. As great as Python is, it is not able to read your mind like that!

不过,您的问题是您希望 Python 理解您想要插入args.fileread一个字符串。尽管 Python 如此伟大,但它无法像那样读懂你的心思!

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 notuse the FileTypetype option! You want a filename, notan open file object:

如果您想将文件名传递给另一个命令,你应该使用FileType类型选择!你想要一个文件名,而不是一个打开的文件对象:

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

But do use subprocess.call()instead of os.system():

但是请使用subprocess.call()代替os.system()

import subprocess

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

If rtl2gdsimplements command line parsing properly, the =is optional and you can use the following call instead, avoiding string concatenation altogether:

如果rtl2gds正确实现命令行解析,则=是可选的,您可以改用以下调用,从而完全避免字符串连接:

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