1

I want to run an executable program which resides in a directory below my script path. The path contains spaces and one the program arguments requires double quotes.

This seems to be a composite of other questions already asked here (Handling directories with spaces Python subprocess.call(), Passing double quote shell commands in python to subprocess.Popen()?) but none of them provided a proper solution for my problem.

Here is my code:

import subprocess

subdir = 'ogr\\'
command = subdir + 'ogr2ogr.exe -f "ESRI Shapefile" output0.shp input0.kml'

output,error = subprocess.Popen(
    command, universal_newlines=True, shell=True, 
    stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

The executable which I am trying to run (ogr2ogr.exe) resides o in a "ogr\" subdirectory below my script, which in turn is in a directory containing spaces (eg., C:\My Documents\My Apps\Scripts\myscript.py). One of its parameters require double quotes. The above commands handle the double quotes in the arguments and work well if I put the script and the input file (input0.kml) in the same subdirectory of the executable, but fail with the message "The system cannot find the specified file" if I attempt to run it in the same directory of the script itself.

Any hints?

Thanks in advance!

maurobio
  • 1,480
  • 4
  • 27
  • 38
  • 1
    Have you tried just using `subprocess.check_output(['ogr2ogr.exe', '-f' ,'ESRI Shapefile', 'output0.shp', 'input0.kml'])` ? It's best to avoid string arguments and use a list of arguments which can automatically be escaped as per the shell requirements intead... – Jon Clements Sep 03 '17 at 14:39
  • Thanks, but this does not work. As I wrote in my question, I have been able to handle the double quotes in the arguments, my problem now is running the subprocess in a subdir containing spaces. – maurobio Sep 03 '17 at 14:47
  • And changing that to be `[r'ogr\ogr2ogr.exe', ...]` doesn't work? Since you're using relative paths - the fact the full path requires quoting doesn't come into it... – Jon Clements Sep 03 '17 at 14:50
  • It'll be worth checking that relative paths make sense and that the current working directory is where you think it is... – Jon Clements Sep 03 '17 at 14:52
  • Your other suggestion also don't work. The script crashes with the same error ("Cannot find the specified file"). – maurobio Sep 03 '17 at 14:53
  • And if you type that exact same command into a command window does it work? I'm guessing no. So get your command line fixed before running it from Python... – Jon Clements Sep 03 '17 at 14:54
  • Why are you using `shell=True`? – Roland Smith Sep 03 '17 at 14:56
  • The first thing that's done on windows is to convert any list argument to string (https://github.com/python/cpython/blob/master/Lib/subprocess.py#L971). The `list2cmdline` function might give you some ideas as to how to escape your parameters (https://github.com/python/cpython/blob/master/Lib/subprocess.py#L424) – thebjorn Sep 03 '17 at 15:08
  • I have similar issue. I think the problem is I don't know exactly how subprocess handles the string passed. I ended up using os.popen(sCommand), which does not solve your problem with subprocess. – Chang Jul 18 '19 at 16:24

0 Answers0