为什么python将windows中的所有环境变量都大写 [英] Why python uppercases all environment variables in windows

查看:33
本文介绍了为什么python将windows中的所有环境变量都大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

os.environ 是否有任何理由在 Windows 上包含所有环境变量大写?,我不明白为什么(仅在 Windows 上)它不使用定义的相同大小写加载它们?

是否有 os.environment 的等效实现来加载环境变量信息而不为 Windows 修改它们?

谢谢

解决方案

因为 Windows 环境变量不区分大小写,但 Python 字典区分大小写.通过将所有条目大写,您可以确保始终能够匹配条目.

引用 Python os.py源代码:

elif name in ('os2', 'nt'): # 环境变量名必须大写# 但我们将它们存储为大写# ...else: # 环境变量名称可以混合大小写的地方

请注意,os.environ 对象将所有访问转换为大写,包括搜索:

def __setitem__(self, key, item):putenv(键,项目)self.data[key.upper()] = itemdef __getitem__(self, key):返回 self.data[key.upper()]# ...def has_key(self, key):在 self.data 中返回 key.upper()def __contains__(self, key):在 self.data 中返回 key.upper()def get(self, key, failobj=None):返回 self.data.get(key.upper(), failobj)

这意味着如果程序找不到os.environ['windir'],则值没有设置.

如果您必须访问原始值,请从 nt 模块中获取它们:

import ntnt.environ

这是操作系统传入的原始初始字典,未更改:

<预><代码>>>>导入>>>排序(nt.environ.keys())['ALLUSERSPROFILE', 'APPDATA', 'COMPUTERNAME', 'ComSpec', 'CommonProgramFiles', 'CommonProgramFiles(x86)', 'CommonProgramW6432', 'FP_NO_HOST_CHECK', 'HOMEDRIVE', 'HOMEPATH', 'LOCALNAPPDATASER'', 'NUMBER_OF_PROCESSORS', 'OS', 'PATHEXT', 'PROCESSOR_ARCHITECTURE', 'PROCESSOR_IDENTIFIER', 'PROCESSOR_LEVEL', 'PROCESSOR_REVISION', 'PROMPT', 'PSModulePath', 'PUBLIC 'ProgramFiles"、ProgramFiles(x86)"、ProgramW6432"、SESSIONNAME"、SSH_AUTH_SOCK"、SystemDrive"、SystemRoot"、TEMP"、TMP"、USERDNSDOMAIN"、USERDOMAIN"、USERNAME", 'USERPROFILE', 'windir', 'windows_tracing_flags', 'windows_tracing_logfile']

Is there any reason why os.environ contains all environment variables uppercase on Windows?, I don't understand why (only on windows) it doesn't load them on using the same case as it is defined ?

Is there an equivalent implementation of os.environment that loads the environment variable information without modifying them for Windows?

thanks

解决方案

Because Windows environment variables are case insensitive, but a Python dictionary is case sensitive. By uppercasing all entries, you ensure that you'll always be able to match entries.

Quoting from the Python os.py source code:

elif name in ('os2', 'nt'):  # Where Env Var Names Must Be UPPERCASE
    # But we store them as upper case

# ... 

else:  # Where Env Var Names Can Be Mixed Case

Note that the os.environ object translates all access to uppercase, including searches:

def __setitem__(self, key, item):
    putenv(key, item)
    self.data[key.upper()] = item
def __getitem__(self, key):
    return self.data[key.upper()]

# ...

def has_key(self, key):
    return key.upper() in self.data
def __contains__(self, key):
    return key.upper() in self.data
def get(self, key, failobj=None):
    return self.data.get(key.upper(), failobj)

This means that if a program fails to find os.environ['windir'], then the value is not set.

If you have to have access to the original values, grab them from the nt module:

import nt
nt.environ

That's the raw initial dictionary as passed in by the OS, unaltered:

>>> import nt
>>> sorted(nt.environ.keys())
['ALLUSERSPROFILE', 'APPDATA', 'COMPUTERNAME', 'ComSpec', 'CommonProgramFiles', 'CommonProgramFiles(x86)', 'CommonProgramW6432', 'FP_NO_HOST_CHECK', 'HOMEDRIVE', 'HOMEPATH', 'LOCALAPPDATA', 'LOGONSERVER', 'NUMBER_OF_PROCESSORS', 'OS', 'PATHEXT', 'PROCESSOR_ARCHITECTURE', 'PROCESSOR_IDENTIFIER', 'PROCESSOR_LEVEL', 'PROCESSOR_REVISION', 'PROMPT', 'PSModulePath', 'PUBLIC', 'Path', 'ProgramData', 'ProgramFiles', 'ProgramFiles(x86)', 'ProgramW6432', 'SESSIONNAME', 'SSH_AUTH_SOCK', 'SystemDrive', 'SystemRoot', 'TEMP', 'TMP', 'USERDNSDOMAIN', 'USERDOMAIN', 'USERNAME', 'USERPROFILE', 'windir', 'windows_tracing_flags', 'windows_tracing_logfile']

这篇关于为什么python将windows中的所有环境变量都大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆