1

I have a problem with passing windows path to a function in python. Now, if I hard code the path everything actually works. So, my code is:

from pymatbridge import Matlab
lab = Matlab(executable=r'"c:\Program Files \MATLAB\bin\matlab.exe"')
lab.start()

This works fine as I am using the raw string formatting to the hard-coded string. Now, the issue is that the string is passed as a variable. So, imagine I have a variable like:

path="c:\Program Files \MATLAB\bin\matlab.exe"

Now, I am unable to figure out how to get the equivalent raw string from this. I tried may things like shlex.quote(path) and this makes issue with the \b. Without conversion to the raw string, the space in Program Files causes a problem, I think.

Luca
  • 9,398
  • 15
  • 84
  • 201
  • 1
    Try "\\" instead of "\" – Abhineet Aug 01 '16 at 11:12
  • use `/` instead, in python also you can use raw string by specifying `path=r'c:\Program Files \MATLAB\bin\matlab.exe'`. – shivsn Aug 01 '16 at 11:13
  • remove `"` or `'`. just use `r'c:\Program Files \MATLAB\bin\matlab.exe'` is OK – Joey.Chang Aug 01 '16 at 11:18
  • Possible duplicate of [whitespaces in the path of windows filepath](http://stackoverflow.com/questions/14852140/whitespaces-in-the-path-of-windows-filepath) – shivsn Aug 01 '16 at 11:18
  • Possible duplicate of [casting raw strings python](http://stackoverflow.com/questions/2428117/casting-raw-strings-python) – SiHa Aug 01 '16 at 11:41

1 Answers1

0
def testpath(path):
    print path

testpath(path='c:\\Program Files \\MATLAB\\bin\\matlab.exe')

output is:

c:\Program Files \MATLAB\bin\matlab.exe

If you are facing issues with space between Program Files use Progra~1 instead

be_good_do_good
  • 3,883
  • 3
  • 27
  • 39