与&符号相关的Python子进程问题 [英] Python subprocess issue with ampersands
问题描述
我目前在使用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:
- 我正在使用子进程模块
- 我将命令行+参数作为列表传递进来
- 问题出在命令本身的路径上参数
- 我尝试引用命令。它导致
[错误123]文件名,目录名或卷标签语法不正确
错误 - 我没有使用shell参数(所以
shell = false
) - 万一重要,我正在抓取一个连接到stderr的管道来处理它,但是忽略了stdout和stdin
- 当前仅在Windows上使用,并且在到目前为止我测试过的所有其他情况下都能正常工作。
- 失败的命令是:
- I am using the subprocess module
- I am passing the command line + arguments in as a list
- The issue is with the path to the command itself, not any of the arguments
- I've tried quoting the command. It causes a
[Error 123] The filename, directory name, or volume label syntax is incorrect
error - I'm using no shell argument (so
shell=false
) - In case it matters, I'm grabbing a pipe to stderr for processing it, but ignoring stdout and stdin
- It is only for use on Windows currently, and works as expected in all other cases that I've tested so far.
- 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屋!