<tfoot id='O2Cz1'></tfoot>
      <bdo id='O2Cz1'></bdo><ul id='O2Cz1'></ul>
    <legend id='O2Cz1'><style id='O2Cz1'><dir id='O2Cz1'><q id='O2Cz1'></q></dir></style></legend>

      <i id='O2Cz1'><tr id='O2Cz1'><dt id='O2Cz1'><q id='O2Cz1'><span id='O2Cz1'><b id='O2Cz1'><form id='O2Cz1'><ins id='O2Cz1'></ins><ul id='O2Cz1'></ul><sub id='O2Cz1'></sub></form><legend id='O2Cz1'></legend><bdo id='O2Cz1'><pre id='O2Cz1'><center id='O2Cz1'></center></pre></bdo></b><th id='O2Cz1'></th></span></q></dt></tr></i><div id='O2Cz1'><tfoot id='O2Cz1'></tfoot><dl id='O2Cz1'><fieldset id='O2Cz1'></fieldset></dl></div>

      <small id='O2Cz1'></small><noframes id='O2Cz1'>

      1. 如何读取 Windows 环境变量值?

        How to read Windows environment variable value?(如何读取 Windows 环境变量值?)

        <small id='fpLMV'></small><noframes id='fpLMV'>

            <i id='fpLMV'><tr id='fpLMV'><dt id='fpLMV'><q id='fpLMV'><span id='fpLMV'><b id='fpLMV'><form id='fpLMV'><ins id='fpLMV'></ins><ul id='fpLMV'></ul><sub id='fpLMV'></sub></form><legend id='fpLMV'></legend><bdo id='fpLMV'><pre id='fpLMV'><center id='fpLMV'></center></pre></bdo></b><th id='fpLMV'></th></span></q></dt></tr></i><div id='fpLMV'><tfoot id='fpLMV'></tfoot><dl id='fpLMV'><fieldset id='fpLMV'></fieldset></dl></div>
              <tbody id='fpLMV'></tbody>

          1. <tfoot id='fpLMV'></tfoot>

            <legend id='fpLMV'><style id='fpLMV'><dir id='fpLMV'><q id='fpLMV'></q></dir></style></legend>
              <bdo id='fpLMV'></bdo><ul id='fpLMV'></ul>

                  本文介绍了如何读取 Windows 环境变量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我试过了:

                  os.environ['MyVar']
                  

                  但它没有用!有没有适合所有操作系统的方法?

                  But it did not work! Is there any way suitable for all operating systems?

                  推荐答案

                  尝试使用以下方法:

                  os.getenv('MyVar')
                  

                  来自文档:

                  os.getenv(varname[, value])

                  os.getenv(varname[, value])

                  如果存在则返回环境变量 varname 的值,如果不存在则返回值.值默认为无.

                  Return the value of the environment variable varname if it exists, or value if it doesn’t. value defaults to None.

                  可用性:大多数版本的 Unix、Windows

                  Availability: most flavors of Unix, Windows

                  所以在测试之后:

                  >>> import os
                  >>> os.environ['MyVar'] = 'Hello World!'       # set the environment variable 'MyVar' to contain 'Hello World!'
                  >>> print os.getenv('MyVar')
                  Hello World!
                  >>> print os.getenv('not_existing_variable')
                  None
                  >>> print os.getenv('not_existing_variable', 'that variable does not exist')
                  that variable does not exist
                  >>> print os.environ['MyVar']
                  Hello World!
                  >>> print os.environ['not_existing_variable']
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in ?
                    File "/usr/lib/python2.4/UserDict.py", line 17, in __getitem__
                      def __getitem__(self, key): return self.data[key]
                  KeyError: 'not_existing_variable    
                  

                  如果环境变量存在,您的方法也可以工作.使用 os.getenv 的区别在于它返回 None (或给定的值),而 os.environ['MyValue'] 给出变量不存在时发生 KeyError 异常.

                  Your method would work too if the environmental variable exists. The difference with using os.getenv is that it returns None (or the given value), while os.environ['MyValue'] gives a KeyError exception when the variable does not exist.

                  这篇关于如何读取 Windows 环境变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)
                    <i id='iaukO'><tr id='iaukO'><dt id='iaukO'><q id='iaukO'><span id='iaukO'><b id='iaukO'><form id='iaukO'><ins id='iaukO'></ins><ul id='iaukO'></ul><sub id='iaukO'></sub></form><legend id='iaukO'></legend><bdo id='iaukO'><pre id='iaukO'><center id='iaukO'></center></pre></bdo></b><th id='iaukO'></th></span></q></dt></tr></i><div id='iaukO'><tfoot id='iaukO'></tfoot><dl id='iaukO'><fieldset id='iaukO'></fieldset></dl></div>
                      <tbody id='iaukO'></tbody>
                          <bdo id='iaukO'></bdo><ul id='iaukO'></ul>
                          <legend id='iaukO'><style id='iaukO'><dir id='iaukO'><q id='iaukO'></q></dir></style></legend>

                          <small id='iaukO'></small><noframes id='iaukO'>

                          • <tfoot id='iaukO'></tfoot>