Convert backward slash to forward slash in python

后端 未结 5 1147
说谎
说谎 2020-12-31 22:40

Hi I have read articles related converting backward to forward slashes. But sol was to use raw string.

But Problem in my case is :

I will get file path dyna

5条回答
  •  难免孤独
    2020-12-31 23:13

    Raw strings are for string literals (written directly in the source file), which doesn't seem to be the case here. In any case, forward slashes are not special characters -- they can be embedded in a regular string without problems. It's backslashes that normally have other meaning in a string, and need to be "escaped" so that they get interpreted as literal backslashes.

    To replace backslashes with forward slashes:

    # Python:
    string = r'C:\dummy_folder\a.txt'
    string = string.replace('\\', '/')
    
    # Ruby:
    string = 'C:\\dummy_folder\\a.txt'
    string = string.gsub('\\', '/')
    

提交回复
热议问题