Python Convert Back Slashes to forward slashes

前端 未结 6 1915
滥情空心
滥情空心 2020-12-15 16:11

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

C:\\folderA\\folderB to C:/folderA/folderB

I have three approaches:

dir = s.replace(\'\\\\         


        
6条回答
  •  半阙折子戏
    2020-12-15 16:31

    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.

提交回复
热议问题