使用子进程模块从Python传递双引号到一个命令 [英] Passing double quotation marks to a command from Python using the subprocess module

查看:198
本文介绍了使用子进程模块从Python传递双引号到一个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我在Python脚本中使用的命令行程序来处理一个内部版本号到可执行文件上。

I have a command line program I'm using from within a Python script to take care of stamping a build number onto an executable.

这是命令行计划: http://www.elphin.com/downloads/stampver/

我的问题是程序在其一个参数中使用双引号(),而我用来调用此程序的Python子进程模块会在双引号前面加上反斜杠执行程序,这会导致程序由于语法不正确而失败。

My problem is that the program takes double quotes (") in one of its arguments and the Python subprocess module I'm using to call this program keeps prepending a backslash onto the double quotes when executing the program. This causes the program to fail due to incorrect syntax.

它期待像下列命令: StampVer.exe -nopad -k -f0.1.4491.0< ExeFile> .exe

而Python正在执行: StampVer.exe -nopad -k -f\0.1.4491.0\< ExeFile> .exe

And instead Python is executing: StampVer.exe -nopad -k -f\"0.1.4491.0\" <ExeFile>.exe

我已经尝试了几件事我在StackOverflow上找到类似的声音问题,例如将字符串标记为原始或在Python之前的引号之前添加反斜杠;这只是在命令行而不是一个三重反斜杠,因为Python然后尝试逃避反斜杠和双引号。

I've tried a few things that I've found here on StackOverflow for similar sounding problems such as marking the string as raw or adding backslashes before the quotes in Python; which just results in triple backslashes on the command line instead of one, because Python then tries to escape the backslash as well as the double quote.

我应该使用其他的东西比完成此过程的子进程模块还是在Python中执行所有这些类型的模块遵循与转义字符相同的规则?有没有什么可以做的,以告诉子进程剥离转义字符,或者根本不添加它们?

Should I be using something other than the subprocess module to accomplish this or do all these types of modules in Python follow the same rules for escaping characters? Is there something I can do to tell subprocess to strip escape characters or to not add them at all?

编辑

这是我从Python调用子进程的方式:

This is how I'm calling subprocess from Python:

def set_file_version(version_number, filepath):

    try:
        file_version_arg = '-f"{0}"'.format(version_number)
        cmd_line = ["StampVer.exe", "-nopad", "-k", file_version_arg, filepath]
        subprocess.check_output(cmd_line)

    except subprocess.CalledProcessError as e:
        if e.returncode == 1:
            pass
        else:
            raise e

StampVer然后返回:

StampVer then returns this:

错误:-f参数无效。尝试-f0.0.0.0使用StampVer - ?帮助

推荐答案

我的解决方案最终变成了一个cop-out。尽管StampVer的文档显示了所有示例中版本号的上述格式,但事实证明,您可以将引号全部放在一起,甚至将其从-f开关中空出,仍将被接受。

My solution ended up being kind of a cop-out. Despite the documentation for StampVer showing the format above for the version number in all examples, it turns out you can just leave the quotes out all together and even space it out from the -f switch and it will still be accepted.

我打算把这个回答称为我的答案,但我仍然认为能够通过子流程传递引号是值得问题的。如果有人有一个答案,实际上可以解决初始问题,那么请张贴,我会标记它。

I'm going to call this my answer but I still think being able to pass quotes through subprocess is a worthwhile problem to figure out. If anyone has an answer that will actually solve the initial problem then please post it and I'll mark it instead.

这篇关于使用子进程模块从Python传递双引号到一个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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