4

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

Alex
  • 581
  • 7
  • 21
  • Can you show us how did you used Popen? with or without shell=True, using string or tuple as command – PasteBT Apr 25 '13 at 21:19
  • how did you see the \? Is that from print(command) or repr(command)? Could you also post the failure msg. – Meitham Apr 25 '13 at 21:19
  • 1
    I'm not using Popen as my edit shows; I'm using check_output with a list of command line strings. I saw the backslashes by instead of calling StampVer.exe directly I created StampVer.bat which called it for me and took the version and exe path as arguments. Then before calling StampVer.exe I echo'ed the version and exe path sent from Python. Additionally I read about an undocumented subprocess function called list2cmdline that will take a command line list and convert it into a command line string. That also contained the backslash characters. – Alex Apr 25 '13 at 21:29

2 Answers2

0

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)
PasteBT
  • 2,128
  • 16
  • 17
  • That script doesn't really work for me on Windows as it returns a WindowsError exception when trying to run sub.py via check_output. I think the reason you're not seeing any backslashes in the printed arguments however is because either Python knows how to handle them correctly when reading in from the command line or, judging by the #! /usr/bin/python at the top of your file, the rules are different on a linux system. – Alex Apr 26 '13 at 14:25
0

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.

Alex
  • 581
  • 7
  • 21