【问题标题】:How to run a python script from another python script?如何从另一个 python 脚本运行 python 脚本?
【发布时间】:2018-02-02 22:01:36
【问题描述】:

我正在尝试从另一个运行 python 脚本,如下所示,一些参数采用quotes ("),如何使用引号运行此命令?

cmd =  r"python ./clone.py -u username -rl %s -ra adar.py -d 13346612 -at "Cloning for automation" -m "Pace" -r "Cloning for automation" "%radar
proc = Popen(cmd.split(' '), stdout=PIPE, stderr=PIPE)

UPDATE#1:我尝试了以下建议,但都没有奏效

  (i)escape the quotes
  (ii)Use multi line quotes
  (iii)Use single quotes as delimiter

更新#2:

我尝试使用 shell=TRUE 运行,但 python 脚本挂起

【问题讨论】:

  • "C....更改为\"C....
  • 一种可能的方法是将命令作为 shell 脚本从 python 运行,Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True )。如果您可以使用它,那么它可能会解决您的问题。
  • @SHAHS - 如果我作为 shell 运行,它只是挂起,还有其他指针吗?

标签: python


【解决方案1】:

您可以使用单引号 ' 来分隔您的字符串,并在字符串中使用双引号 " 来分隔您的参数。

【讨论】:

  • 这不起作用,它抛出错误error: unrecognized arguments: for automation" for automation
【解决方案2】:

使用multi-line string

将 cmd 用每侧 3 个单引号括起来:

即:

cmd =  ''' 
python ./clone.py -u username -rl {} -radar.py -d
13346612 -at "Cloning for automation" -m "Pace" -r "Cloning for
automation"  
'''.format(radar)

【讨论】:

  • 这也不起作用,Popen 无法运行多行字符串
  • `proc = Popen(cmd.split(''), stdout=PIPE, stderr=PIPE) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2. 7/subprocess.py”,第 710 行,在 init errread, errwrite) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py” ,第 1335 行,在 _execute_child raise child_exception OSError: [Errno 13] Permission denied` 是确切的错误
猜你喜欢
  • 2016-09-03
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多