以下字符串在命令行中有效,我想将其括在外部引号中以将其用作 Python 应用程序中的字符串,但不知道如何对其进行转义。有人可以帮忙吗,或者有应用程序或网站可以帮我做这件事吗?
powershell -Command "&cmd.exe -ArgumentList '/c \"\"c:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe\" d:\xx\Code\VisualStudio\my_project\my_project.sln /Target:Rebuild /p:Configuration=Debug /p:Platform=x64 /p:BuildInParallel=false\"'"
将命令放入文本文件中。使用 Python 打开文件并将其内容读入字符串(
.strip()
去掉尾随换行符),然后使用repr()
以获取适合在 Python 源代码中使用的 Python 化表示。repr() 并不适用于所有类型(大多数第三方类都不是),但它适用于字符串/字典/列表。
[OP,擅自编辑答案以包含代码,希望将来能对某人有所帮助]
因此,现在
subprecess.run('MSBuild.bat')
我可以使用subprecess.run(<valeu of escaped_command>)