mixed slashes with os.path.join on windows

前端 未结 7 1344
一生所求
一生所求 2020-11-27 04:39

I tend to use only forward slashes for paths (\'/\') and python is happy with it also on windows. In the description of os.path.join it says that is the correct way if you w

相关标签:
7条回答
  • 2020-11-27 05:07

    You can also do this:

    import re
    
    a = 'c:/'
    b = 'myFirstDirectory/'
    c = 'mySecondDirectory'
    d = 'myThirdDirectory'
    e = 'myExecutable.exe'
    
    joined = os.path.join(a, b, c, d, e)
    formatted = re.sub(r'/|\\', re.escape(os.sep), joined)
    

    This is going to switch all your potentially mixed slashes into OS compliant ones.

    I know it's an ancient topic but I couldn't resist. :)

    0 讨论(0)
提交回复
热议问题