How to use “/” (directory separator) in both Linux and Windows in Python?

前端 未结 9 1659
日久生厌
日久生厌 2020-11-28 19:43

I have written a code in python which uses / to make a particular file in a folder, if I want to use the code in windows it will not work, is there a way by which I can use

相关标签:
9条回答
  • 2020-11-28 20:12

    You can use os.sep:

    >>> import os
    >>> os.sep
    '/'
    
    0 讨论(0)
  • 2020-11-28 20:13

    os.path.normpath(pathname) should also be mentioned as it converts / path separators into \ separators on Windows. It also collapses redundant uplevel references... i.e., A/B and A/foo/../B and A/./B all become A/B. And if you are Windows, these all become A\B.

    0 讨论(0)
  • 2020-11-28 20:13

    Some useful links that will help you:

    • os.sep
    • os.path
    • os.pathsep
    0 讨论(0)
提交回复
热议问题