0

So I have following problem:

"L:\GDrive\test\img.png".replace(os.sep, '/')

returning

"L:/GDrive\test/img.png"

Is there a way to replace all \ characters without manually going over all possible special symbols like \t? Need to preserve t of course.

Il'ya Zhenin
  • 1,272
  • 2
  • 20
  • 31
  • 3
    Your string does not contain three backslash characters, it contains only two. `\t` inside a string is interpreted as a tab character. Use `r"L:\GDrive\test\img.png"` to get the string you actually meant. – Thomas Jan 13 '22 at 10:24
  • `\t` in a string literal means "tab", not "backslash t". – user2357112 Jan 13 '22 at 10:24
  • Personally, I'd use Pathlib here rather than using os.sep to join paths. Pathlib will allow you to create a path in an OS independent way and at the same time, won't strip out any escaped symbols (since you aren't using / or \) – scotty3785 Jan 13 '22 at 11:17

1 Answers1

1

try use "r" before the path:

r"L:\GDrive\test\img.png".replace(os.sep, '/')

output:

'L:/GDrive/test/img.png'
Tal Folkman
  • 2,368
  • 1
  • 7
  • 21