How to get the PATH environment-variable separator in Python?

前端 未结 5 865
粉色の甜心
粉色の甜心 2020-12-02 12:43

When multiple directories need to be concatenated, as in an executable search path, there is an os-dependent separator character. For Windows it\'s \';\', for

相关标签:
5条回答
  • 2020-12-02 13:11

    os.pathsep

    0 讨论(0)
  • 2020-12-02 13:13

    It is os.pathsep

    0 讨论(0)
  • 2020-12-02 13:23

    Making it a little more explicit (For python newbies like me)

    import os
    print(os.pathsep)
    
    0 讨论(0)
  • 2020-12-02 13:30

    OK, so there are:

    • os.pathsep that is ; and which is a separator in the PATH environment variable;
    • os.path.sep that is / in Unix/Linux and \ in Windows, which is a separator between path components.

    The similarity is a source of confusion.

    0 讨论(0)
  • 2020-12-02 13:31

    This is a sample path for your working directory/specific folder -

     import os
     my = os.path.sep+ "testImages" + os.path.sep + "imageHidden.png"
     print(my)
    

    Output for Linux-

    /home/*******/Desktop/folder/PlayWithPy/src/testImages/imageHidden.png

    Output for Windows-

    C:\\Users\\Administrator\\Desktop\\folder\\tests\\testImages\\imageHidden.png

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