通过使用Python的参数,包括双引号到子过程
我正在寻找一种传递参数的方法,该参数包含python的子过程模块的双重引号,而没有被逃脱的双引号。
我已经看到这个问题问了几个不同的时间围绕着arg,但答案通常是要删除报价。 参数的一部分
报价是
我点击的问题是, 它:
echo "%~1"
我调用Python的批处理脚本,其中包括引号:
import subprocess
test_string = 'Bill said "Hi!"'
cmd = ["subprocesTest.bat",
test_string
]
p = subprocess.Popen(cmd)
p.wait()
预期输出:
“比尔说“嗨!”“
实际输出:
“比尔说\”嗨!\“”
“
subprecestest.bat“ bill说“嗨!””
查看来源,subprocess.py的list2cmdline函数,记录在:
3) A double quotation mark preceded by a backslash is
interpreted as a literal double quotation mark.
但是,如果我逐步介绍此功能,我不会看到在不准备后斜线的情况下传递双引号:
elif c == '"':
# Double backslashes.
result.append('\\' * len(bs_buf)*2)
bs_buf = []
result.append('\\"')
所以我有两个问题...
- 可以将ARG传递给子进程,其中包含双重 引用wihtout预备后斜线,
- 如果不可能,有人可以向我解释为什么该功能 是这样设计的吗? Funcion的DOC字符串中的第三项 似乎表明我可以通过准备 后斜线,但这并不能产生我一直在寻找的行为。 我在读错文档吗?
以下是问这个问题的其他线程,并通过不使用引号来解决:
我看到我实际上想通过论点中传递报价的情况没有回应
I'm looking for a way to pass an argument which contains double quotes to python's subprocess module, without the double quotes being escaped.
I've seen this question asked a few different times when the quotes are surrounding the arg, but the answer is usually to remove the quotes. The issue I've hit is that the quotes are part of the argument and must be passed to the script without prepending a back slash to each quote
example:
I'm calling subprocesTest.bat, a simple batch script that takes an argument and echos it:
echo "%~1"
I invoke the batch script from python with an arg that includes quotes:
import subprocess
test_string = 'Bill said "Hi!"'
cmd = ["subprocesTest.bat",
test_string
]
p = subprocess.Popen(cmd)
p.wait()
expected output:
"Bill said "Hi!""
actual output:
"Bill said \"Hi!\""
I can get the expected output when calling the same batch script from the command line as follows:
subprocesTest.bat "Bill said "Hi!""
Looking at the source, subprocess.py's list2cmdline function is documented with:
3) A double quotation mark preceded by a backslash is
interpreted as a literal double quotation mark.
However, if I step through this function, I see no means of passing a double quote through without prepending a backslash:
elif c == '"':
# Double backslashes.
result.append('\\' * len(bs_buf)*2)
bs_buf = []
result.append('\\"')
So I have two questions...
- Is it possible to pass an arg to subprocess which contains a double
quote wihtout prepending a backslash - If it is not possible, can someone explain to me why the function
was designed this way? Item three in the funcion's doc string
seems to indicate that I can pass a literal quote by prepending a
backslash, but this doesn't yield the behavior I was looking for.
Am I reading the doc wrong?
Here are other threads where this question is asked, and worked around by not using quotes:
- Passing double quotation marks to a command from Python using the subprocess module
- How can I tell subprocess to stop escaping my quotes?
I see no response for a situation where I actually want to pass quotes in my argument
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)是的,但也许不在Windows和/或.bat文件上。例如,当您运行等同于以下窗口的窗口时会发生什么:
myprog.py:
2)好吧,以下内容似乎有些模棱两可:
引用的字符串是:
接着是未引用的字符串:
随后是引用的空白字符串:
?
1) Yes, but maybe not on windows and/or with a .bat file. For example, what happens when you run the windows equivalent of this:
myprog.py:
2) Well, the following seems a bit ambiguous:
Is that the quoted string:
followed by the unquoted string:
followed by a quoted blank string:
?