如何在Python中获得路径环境变量分隔符?

时间:2022-06-03 11:59:24

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 Linux it's ':'. Is there a way in Python to get which character to split on?

当需要连接多个目录时(如在可执行搜索路径中),有一个与操作系统相关的分隔符。对于Windows来说是“;”,对于Linux来说是“:”。Python中是否有一种方法来获取要分割的字符?

In the discussions to this question How do I find out my python path using python? , it is suggested that os.sep will do it. That answer is wrong, since it is the separator for components of a directory or filename and equates to '\\' or '/'.

在讨论这个问题时,我如何使用python找到我的python路径?,建议使用os。9月将这样做。这个答案是错误的,因为它是目录或文件名组件的分隔符,等于'\ '或'/'。

3 个解决方案

#1


204  

os.pathsep

os.pathsep

#2


31  

It is os.pathsep

这是os.pathsep

#3


11  

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

让它更明确一些(对于像我这样的python新手来说)

import os
print(os.pathsep)

#1


204  

os.pathsep

os.pathsep

#2


31  

It is os.pathsep

这是os.pathsep

#3


11  

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

让它更明确一些(对于像我这样的python新手来说)

import os
print(os.pathsep)