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
  • 115,249
  • 20
  • 229
  • 336
Gala
  • 2,592
  • 3
  • 25
  • 33

2 Answers2

8

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
  • 63,191
  • 45
  • 217
  • 228
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
  • 63,191
  • 45
  • 217
  • 228
Edd Saunders
  • 138
  • 7