Python – os.popen Does Not Work with Parameters Inside Quotes When Command is in Quotes

To be honest, all I really know is that this python code returns ” for me,

[Dbg]>>> print command
"C:\automated tasks\GoldMine_Replace\GoldMine_Replace.exe" -lg -pfd5 -r"3XDYQ2#PK [Dbg]>>> os.popen(command).read()
''

while running the same command at the DOS prompt returns good stuff.
C:\>"C:\automated tasks\GoldMine_Replace\GoldMine_Replace.exe" -lg -pfd5 -r"3XDYQ2#PK Success

Additionally this Python bug seems to imply that os.system and the os.popen's just don't work quite right on Windows.

But don't get frusterated. Just do a cheap workaround and move/rename your executable (put it in a batch file if you have to) so that you don't need to put the command in quotes. That means making it have no spaces in it's filepath. That's what worked for me. I just moved the executable GoldMine_Replace.exe to the C:\ directory.

There's also a bit of discussion here but no real insight (just people claiming this problem doesn't exist?)

4 Responses to “Python – os.popen Does Not Work with Parameters Inside Quotes When Command is in Quotes”

  1. Fredrik says:

    That’s a well-known bug in *Windows* command-line parser. The work-around is also well-known: just add an *extra* quote at the beginning of the line:

    “”\program files\big corporation\program.exe” “arg”

  2. Fredrik, for some reason the extra quote didn’t work for me in this case. I’m not sure why. Nonetheless the extra quote is the best work-around to try first.

  3. Fuzzyman says:

    I *thought* that the solution to this bug was to add extra quotes as follows :

    “”\program files\big corporation\program.exe” “arg”"

    One extra at the start and one at the end.

    An alternative is to use the win32api call win32api.GetShortName (I think that is correct – something like that anyway) so that the filename doesn’t need quoting.

  4. Good ideas, Fuzzyman. Those are certainly worth trying. BTW, the “big corporation” in the file path amuses me for some reason ;-)