与&符号相关的Python子进程问题 [英] Python subprocess issue with ampersands

查看:43
本文介绍了与&符号相关的Python子进程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在使用python脚本时遇到重大问题。该脚本通过处理程序运行任意命令,以将错误的错误报告转换为正确的错误报告。

I'm currently having a major issue with a python script. The script runs arbitrary commands through a handler to convert incorrect error reporting into correct error reporting.

我遇到的问题是使脚本在Windows上正常运行。路径中包含&号的命令。我试图引用该命令,并用^来代替&号,但均无效。我现在没主意了。有什么建议吗?

The issue I'm having is getting the script to work correctly on windows with a command that contains ampersands in it's path. I've attempted quoting the command, escaping the ampersand with ^ and neither works. I'm now out of ideas. Any suggestions?

要从当前答复中进行澄清:

To clarify from current responses:


  1. 我正在使用子进程模块

  2. 我将命令行+参数作为列表传递进来

  3. 问题出在命令本身的路径上参数

  4. 我尝试引用命令。它导致 [错误123]文件名,目录名或卷标签语法不正确错误

  5. 我没有使用shell参数(所以 shell = false

  6. 万一重要,我正在抓取一个连接到stderr的管道来处理它,但是忽略了stdout和stdin

  7. 当前仅在Windows上使用,并且在到目前为止我测试过的所有其他情况下都能正常工作。

  8. 失败的命令是:

  1. I am using the subprocess module
  2. I am passing the command line + arguments in as a list
  3. The issue is with the path to the command itself, not any of the arguments
  4. I've tried quoting the command. It causes a [Error 123] The filename, directory name, or volume label syntax is incorrect error
  5. I'm using no shell argument (so shell=false)
  6. In case it matters, I'm grabbing a pipe to stderr for processing it, but ignoring stdout and stdin
  7. It is only for use on Windows currently, and works as expected in all other cases that I've tested so far.
  8. The command that is failing is:




p = subprocess.Popen(prog,stderr = subprocess.PIPE ,bufsize = -1)

p = subprocess.Popen(prog, stderr = subprocess.PIPE, bufsize=-1)

当列表 prog的第一个元素包含&号时。引用第一个字符串不起作用。

when the first element of the list 'prog' contains any ampersands. Quoting this first string does not work.

推荐答案

请确保您使用的是列表且没有shell扩展:

Make sure you are using lists and no shell expansion:

subprocess.Popen(['command', 'argument1', 'argument2'], shell=False)

这篇关于与&符号相关的Python子进程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆