6

I need to pass a subtitle path to VLC, it only takes native paths (backslashes on Windows, forward slashes on Unix) and needs space escaping.

Let's say I have a Qt native path with a space in it.

C:/Users/Thinkpad/Downloads/test file.srt

How do I convert it into this:

C:\\Users\\Thinkpad\\Downloads\\test\ file.srt

ekhumoro
  • 107,367
  • 18
  • 208
  • 308
Gala
  • 2,324
  • 3
  • 20
  • 29

2 Answers2

7

To handle this problem I strongly suggest using

os.path.normpath('C:/Users/Thinkpad/Downloads/test file.srt')

If you enter all of your filename strings using forward slashes, and then let os.path.normpath(path) change them to backslashes for you, this way.

lmiguelvargasf
  • 51,786
  • 40
  • 198
  • 203
3

Not sure if there is anything in the standard library to handle this, but if it is just slashes and spaces you need a simple string replace will be faster and simpler. i.e.

path = path.replace('/','\\').replace(' ','\ ')
lmiguelvargasf
  • 51,786
  • 40
  • 198
  • 203
Edd Saunders
  • 138
  • 7