2017-06-20 166 views
0

我想从另一个python脚本运行一个python脚本,但由于传递的参数中有一个空格而被阻塞。我试图运行从名为命令终端运行的脚本和论据,这样运行一个包含另一个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") 

,但我得到无效的语法

+0

你需要躲避空间:'使用os.system( 'python Duplicate_Checki.py“Google \ Control \ Center 7.5 \ Hardening'')' – inspectorG4dget

+1

看看'subprocess.run'。它不会在空间上分裂。 – Artyer

回答

2

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

+0

我运行的是Python 2.7版本,所以我用'call([“Duplicate_Checki.py”,“Google Control Center”,“7.5 Hardening”],shell = True)'' –

相关问题