【问题标题】:Python: Pass a "<" to PopenPython:将“<”传递给 Popen
【发布时间】:2017-09-02 11:48:23
【问题描述】:

我想用参数启动ReadyApi的testrunner.bat。我尝试将一些 XML 部分(下面代码中的 PeriodEnd)作为参数传递给 subprocess.Popen:

argslist = ['C:/Program Files/SmartBear/ReadyAPI-1.9.0/bin/testrunner.bat',
    '-a', '-s', 'TestSuite', '-c', 'TestCase', '-f', 'C:/temp/', '-P', 
    'PeriodEnd=<PeriodEnd>2017-04-11T00:00:00.000Z</PeriodEnd>', 
    'C:/temp/soapui-project.xml']
proc = Popen(argslist, stdout=PIPE, stderr=PIPE)

这会产生以下错误:

The system cannot find the file specified.

我发现“”是问题所在。我怎样才能逃脱它们或将它们传递给 Popen?

【问题讨论】:

  • 我猜 Windows 希望您在该特定参数周围加上双引号,但我没有 Wintendo 框来测试它。
  • 您是否尝试直接在 CMD 中运行该命令?
  • 我也尝试使用 '""PeriodEnd=2017-04-11T00:00:00.000Z""' 但这没有帮助。
  • 是的,我直接在 CMD 中尝试过,它可以工作。

标签: python windows python-2.7 subprocess popen


【解决方案1】:

CMD 中的转义字符是^

C:\> echo asdf<123>
The syntax of the command is incorrect.

C:\> echo asdf^<123^>
asdf<123>

【讨论】:

  • 是的,子进程list2cmdline 使用VC++ 规则来创建命令行。它不知道如何为 cmd.exe 转义命令行,特别是考虑到它们通常必须经过精心设计才能在 cmd 以及通过 cmd 运行的任何内容的命令行解析中存活。如果您使用Popen 直接运行批处理文件或使用shell=True,我建议使用经过验证的命令行字符串而不是args 列表。
【解决方案2】:

\用于转义字符,试试\&lt;

https://docs.python.org/2.0/ref/strings.html

可能使用" 而不是'

【讨论】:

  • 尖括号在 Python 中不需要转义。这是一个 Windows CMD 问题
猜你喜欢
  • 2016-10-27
  • 2012-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-26
  • 1970-01-01
  • 2014-07-10
  • 2023-03-03
相关资源
最近更新 更多