简体   繁体   中英

Windows path with spaces in python

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM