'How to get my path written with back slashes instead of forward slashes?

I hope someone can help as I am stuck, I can't find the answer to this problem anywhere on google.

I need to replace my forward slash path with a backslash path in order for it to work with windows command prompt. Forward slash paths work for local folders, i.e. C:/Users/Lorcan - but not network folders, i.e. //Networkfolder/Storage

I learned that you can't use the backslash in python as it is a special character, so you must use two backslashes. However, this causes my path to have too many backslashes, and command prompt doesn't work.

>>> s = '//Networkfolder/Storage/Myfolder/Myfile'
>>> s2 = s.replace('/','\\')
>>> s2
'\\\\Networkfolder\\Storage\\Myfolder\\Myfile'


Solution 1:[1]

In the python shell, backslashes are displayed as \\, but it's really \ in the string. Your code is working fine, the real string is correct, it's being displayed like that.

Solution 2:[2]

You can print out your current working directory instead of writing it out:

import os

cwd = str(os.getcwd())
x = cwd.replace("/", "\\")
print(x)

This worked for me, hope it does for you as well!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 sidereal