Passing double quotation marks to a command from Python using the subprocess module

孤者浪人 提交于 2020-01-14 22:45:25

问题


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.

This is the command line program: http://www.elphin.com/downloads/stampver/

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.

It is expecting a command like: 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

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.

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?

EDIT

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 then returns this:

error: Invalid -f parameter. Try -f"0.0.0.0" Use StampVer -? for help


回答1:


try this script sub.py:

#! /usr/bin/python

import sys
from subprocess import check_output

if len(sys.argv) > 1:
    print sys.argv
else:
    print check_output((sys.argv[0], '-f"1234"'))

then run it:

./sub.py

it return what we gave:

['./sub.py', '-f"1234"']

So I guess check_output works just fine, the problem may came from how StampVer.exe handle parameter, you can try

file_version_arg = '-f{0}'.format(version_number)



回答2:


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.



来源:https://stackoverflow.com/questions/16224868/passing-double-quotation-marks-to-a-command-from-python-using-the-subprocess-mod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!