python 与系统无关地确定系统 PATH 系统



我正在编写的程序需要确定系统上是否安装了某些其他程序。 特别是用于生成哈希值的命令行程序。 由于这些程序有很多可能的版本,我只需要检查主要版本(md5、whirlpool 等)。

我正在编写的程序与系统无关,并且打算在任何win/mac/*nix上运行。

我希望能够快速搜索操作系统的标准$PATH,但我不知道如何在系统之间检索该信息($PATH的内容)。

这里的搜索只发现了关于查找python路径或当前正在运行的脚本路径的材料。

有人会有一个解决方案,或者能够指出我跨平台的解决方案吗?

这应该是跨平台的,除非我忽略了一些明显的东西:

Linux示例:

Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.environ['PATH']
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

MacOS 示例:

Python 2.7.2 (default, Oct 11 2012, 20:14:37) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.environ['PATH']
/Users/vlazarenko/bin:/Users/vlazarenko/SDK/QtSDK/Desktop/Qt/474/gcc/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

视窗示例:

Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.environ['PATH']
C:Program Files (x86)ParallelsParallels ToolsApplications;C:Windowssystem3
2;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0
;C:Program Files (x86)Microsoft SQL Server100ToolsBinn;C:Program FilesM
icrosoft SQL Server100ToolsBinn;C:Program FilesMicrosoft SQL Server100DT
SBinn;C:Program Files (x86)Gitcmd;C:Program Files (x86)Microsoft SQL Serv
er100ToolsBinnVSShellCommon7IDE;C:Program Files (x86)Microsoft Visual S
tudio 9.0Common7IDEPrivateAssemblies;C:Program Files (x86)Microsoft SQL Se
rver100DTSBinn
>>>

最新更新