Vista64 机器上的 Python 2.4.3。
环境中有以下2个变量:
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
但是当我运行以下命令时
import os
print os.environ["ProgramFiles"]
print os.environ["ProgramFiles(x86)"]
我得到:
C:\Program Files (x86)
C:\Program Files (x86)
知道如何获得“ProgramFiles”的正确值吗?
最佳答案
来自Wikipedia page :
%ProgramFiles%
This variable points to Program Files directory, which stores all the installed program of Windows and others. The default on English-language systems is C:\Program Files. In 64-bit editions of Windows (XP, 2003, Vista), there are also %ProgramFiles(x86)% which defaults to C:\Program Files (x86) and %ProgramW6432% which defaults to C:\Program Files. The %ProgramFiles% itself depends on whether the process requesting the environment variable is itself 32-bit or 64-bit (this is caused by Windows-on-Windows 64-bit redirection).
因此,为了仅获取 C:\Program Files,您显然需要检查 %ProgramW6432%
。
关于Python 在 64 位 Vista 上获得 os.environ ["ProgramFiles"] 的错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1283664/