How to convert back-slashes to forward-slashes?

Question:

I am working in python and I need to convert this:

C:folderAfolderB to C:/folderA/folderB

I have three approaches:

dir = s.replace('\','/')

dir = os.path.normpath(s) 

dir = os.path.normcase(s)

In each scenario the output has been

C:folderAfolderB

I’m not sure what I am doing wrong, any suggestions?

Asked By: John87

||

Answers:

Try

path = '/'.join(path.split('\'))
Answered By: TheoretiCAL

Your specific problem is the order and escaping of your replace arguments, should be

s.replace('\', '/')

Then there’s:

posixpath.join(*s.split('\'))

Which on a *nix platform is equivalent to:

os.path.join(*s.split('\'))

But don’t rely on that on Windows because it will prefer the platform-specific separator. Also:

Note that on Windows, since there is a current directory for each
drive, os.path.join(“c:”, “foo”) represents a path relative to the
current directory on drive C: (c:foo), not c:foo.

Answered By: Jason S

How about :

import ntpath
import posixpath
.
.
.
dir = posixpath.join(*ntpath.split(s))
.
.
Answered By: T P Saravanan

To define the path’s variable you have to add r initially, then add the replace statement .replace('\', '/') at the end.

for example:

In>>  path2 = r'C:UsersUserDocumentsProjectEm2Lph'.replace('\', '/')
In>>  path2 
Out>> 'C:/Users/User/Documents/Project/Em2Lph/'

This solution requires no additional libraries

Answered By: Mohammad ElNesr

I recently found this and thought worth sharing:

import os

path = "C:\tempmyFolderexample\"
newPath = path.replace(os.sep, '/')
print(newPath)  # -> C:/temp/myFolder/example/
Answered By: Numabyte

Path names are formatted differently in Windows. the solution is simple, suppose you have a path string like this:

data_file = "/Users/username/Downloads/PMLSdata/series.csv"

simply you have to change it to this: (adding r front of the path)

data_file = r"/Users/username/Downloads/PMLSdata/series.csv"

The modifier r before the string tells Python that this is a raw string. In raw strings, the backslash is interpreted literally, not as an escape character.

Answered By: scapa

Sorry for being late to the party, but I wonder no one has suggested the pathlib-library.

pathlib is a module for "Object-oriented filesystem paths"

To convert from windows-style (backslash)-paths to forward-slashes (as typically for Posix-Paths) you can do so in a very verbose (AND platform-independant) fashion with pathlib:

import pathlib

pathlib.PureWindowsPath(r"C:folderAfolderB").as_posix()
>>> 'C:/folderA/folderB'

Be aware that the example uses the string-literal "r" (to avoid having "" as escape-char)
In other cases the path should be quoted properly (with double backslashes) "C:\folderA\folderB"

Answered By: Stefan

This can work also:

def slash_changer(directory):

if "\" in directory:
    return directory.replace(os.sep, '/')
else:
    return directory

print(slash_changer(os.getcwd()))

Answered By: Miroslav Kopecký

this is the perfect solution put the letter ‘r’ before the string that you want to convert to avoid all special characters likes ‘t’ and ‘f’…
like the example below:

str= r"testhhd"

print("windows path:",str.replace("\","\\"))
print("Linux path:",str.replace("\","/"))

result:

windows path: \test\hhd
Linux path: /test/hhd
Answered By: Chafik Boulealam
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.