[英]Running a python script with arguments that contain space from another python script
我试图从另一个python脚本运行python脚本,但是由于传递的参数中有空格,我被阻止了。 我尝试运行的脚本是从命令终端运行的,其名称和参数是这样的
>>>Duplicate_Checki.py "Google Control Center" "7.5 Hardening"
在我尝试调用第一个脚本的脚本中,代码如下所示:
def run_duplicate_check(self):
os.system("python Duplicate_Checki.py Google Control Center 7.5 Hardening")
我收到以下错误
Duplicate_Checki.py: error: unrecognized arguments: Center 7.5 Hardening
还尝试了os.system("python Duplicate_Checki.py {} {}".format("Google Control Center" ,"7.5 Hardening"))
,但存在相同的错误
我也尝试过
os.system(python Duplicate_Checki.py "Google Control Center" "7.5 Hardening")
但我得到无效的语法
script.py :
import sys
if __name__ == '__main__':
args = sys.argv[1:]
print(args[0])
print(args[1])
Runner.py :
from subprocess import call
call(["python3", "script.py", "Google Control Center", "7.5 Hardening"])
执行python3 runner.py
,输出:
Google Control Center
7.5 Hardening
参见https://docs.python.org/3/library/subprocess.html
#subprocess.run,#subprocess.check_output,#subprocess.call
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.