Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
727 views
in Technique[技术] by (71.8m points)

replace - Python Convert Back Slashes to forward slashes

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I recently found this and thought worth sharing:

import os

path = "C:\tempmyFolderexample"

newPath = path.replace(os.sep, '/')

print newPath


Output:<< C:/temp/myFolder/example/  >>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...