Python无关系统地确定系统PATH
我正在写一个程序,需要判断系统上是否安装了某些其他程序。具体来说,我需要一个命令行程序来生成哈希值。因为这些程序有很多不同的版本,所以我只想检查一些主要的版本,比如md5、whirlpool等。
我写的这个程序不依赖于特定的操作系统,旨在能够在Windows、Mac和各种Linux系统上运行。
我想快速搜索操作系统的标准$PATH,但我不知道如何从不同的系统中获取这个信息($PATH的内容)。
我查找了一下,发现大多数资料只是在讲如何找到Python的路径,或者当前运行脚本的路径。
有没有人能提供一个解决方案,或者指引我找到一个跨平台的解决办法呢?
1 个回答
5
这段内容应该在不同的平台上都能用,除非我忽略了什么明显的问题:
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
Windows 的例子:
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)\Parallels\Parallels Tools\Applications;C:\Windows\system3
2;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0
\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\M
icrosoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DT
S\Binn\;C:\Program Files (x86)\Git\cmd;C:\Program Files (x86)\Microsoft SQL Serv
er\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual S
tudio 9.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Se
rver\100\DTS\Binn\
>>>