Home > Mobile >  How to get my path written with back slashes instead of forward slashes?
How to get my path written with back slashes instead of forward slashes?

Time:04-12

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'

CodePudding user response:

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.

CodePudding user response:

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!

  • Related