由于字符转义序列问题,我无法运行批处理命令。
从python输入的:
import subprocess
print(data) ==> --i "test - testing"
subprocess.call(["c:/foo/boo/file.bat", data])
批处理文件:
SET @tt=%1
输出:-
C:\foo\boo>SET @tt=" --i \"test
预期:-
C:\foo\boo>SET @tt=--i "test - testing"
是否有一种方法可以转义空白以在批处理文件中传递实际输入?请给我建议。
发布于 2021-01-26 14:28:06
命令的正确引用和转义可能是很棘手的。Python在一个模块中有一个函数来帮助简化:shlex.split
:❝使用类似shell的语法拆分字符串。❞
文档:shlex.split
我没有办法测试你的代码。以下是我认为你正在努力实现的目标的一个例子。
import shlex
import subprocess
command = 'c:/foo/boo/file.bat --i "test - testing"'
split_command = shlex.split(command)
print(split_command) # shlex.split handles all the proper escaping
subprocess.call(split_command)
打印输出:['c:/foo/boo/file.bat', '--i', 'test - testing']
https://stackoverflow.com/questions/65902994
复制相似问题