In VBA code, I am calling Python and passing an argument successfully like this when there are no spaces in the path to the python file:
Dim wsh As Object
Set wsh = CreateObject("WScript.Shell")
Dim shell_exec As Object
Set shell_exec = wsh.Exec("C:\path_to_executable\python.exe " & "C:\path_without_spaces\file.py " & arg)
However, I cannot find any way to pass an argument to python when the path to the python file contains spaces. For example, triple double-quoting the path works when there is no argument:
Set shell_exec = wsh.Exec("C:\path_to_executable\python.exe " & """C:\path with spaces\file.py""")
But I am unable to find any way to pass an argument when doing this. For example, calls like this (and every variation on this theme I can think of) fail:
Set shell_exec = wsh.Exec("C:\path_to_executable\python.exe " & """C:\path with spaces\file.py """ & arg)
Is this possible, or is the use of spaces fatally ambiguous in this situation?
Advice is appreciated.
1 Answer 1
Try to use multiple quotes:
Set shell_exec = wsh.Exec("""C:\path_to_executable\python.exe """ & """C:\path with spaces\file.py """ & arg)
python.exeRunmethod seems easier to use with arguments with spaces. I'm not sure why.Runbut fail withExec. How badly do you need to have spaces in the path? Personally, I keep most of my programs in a folder in my documents (in a path which contains spaces) but also keep a folder close to the root directory for programs that I need to invoke from the command line (which isn't a very common use-case for me). You could create such a folder and move a copy of your script to it.