0

I have a string containing a filepath for Windows:

import os
dir = os.getcwd()
# bogus name
'C:\\my\\folder'

If I copy and paste dir in Windows Explorer or other programs, I get an error, the file name is not valid.

I tried the solutions in this question but none worked. The closest I got is:

dir.replace('\\', '/')
'C:/my/folder'

Now it works for some windows programs but not others, so I simply want to replace '\\' by '\'

Brenlla
  • 1,471
  • 1
  • 11
  • 23

1 Answers1

0

In case you're in a python shell and just evaluated the dir, it's sufficient to print(dir).

Otherwise you'd want to do print(dir.replace('\\\\', '\\')), which seems to be a very strange output from os.getcwd()

Keep in mind that '\' is an escape character and thus needs to be escaped itself.

Here's an example from Python shell:

>>> a = 'c:\\foo\\bar'
>>> a
'c:\\foo\\bar'
>>> print(a)
c:\foo\bar
Antricks
  • 171
  • 1
  • 9