Detailed explanation of os.environ module environment variables

1. Detailed explanation of os.environ()

Various information about the system can be obtained through os.environ in python

1.1 Introduction

Get environment variables through os.environ. What are environment variables? Environment variables are the means of communication between the program and the operating system. Some characters should not be written into the code in plain text, such as database passwords and personal account passwords. If they are written into the environment variables of their own machine, they can be retrieved through os.environ.get() when the program is used. In this way, developers use their own set of passwords when testing the machine, and when deploying in the production environment, they use the company's public account and password, which can increase security. os.environ is a dictionary, a dictionary of environment variables . "HOME" is a key in this dictionary, if there is this key, return the corresponding value, if not, return none

2. Detailed explanation of the key field

  • os.environ.keys() All keys in the home directory
    Insert picture description here

1.2 Common key fields

windows:

os.environ['HOMEPATH']:当前用户主目录。
os.environ['TEMP']:临时目录路径。
os.environ["PATHEXT"]:可执行文件。
os.environ['SYSTEMROOT']:系统主目录。
os.environ['LOGONSERVER']:机器名。
os.environ['PROMPT']:设置提示符。

linux:

os.environ['USER']:当前使用用户。
os.environ['LC_COLLATE']:路径扩展的结果排序时的字母顺序。
os.environ['SHELL']:使用shell的类型。
os.environ['LAN']:使用的语言。
os.environ['SSH_AUTH_SOCK']:ssh的执行路径。

1.3 Usage of os.environ.get()

os.environ is a dictionary, a dictionary of environment variables , you can get the value corresponding to the key through the get method (note that the type of os.environ is not <class'dict'>, not all dictionary functions can be used)

os.environ.get() is a method of os module in python to get environment variables. If there is this key, return the corresponding value, if not, return none

import os
print(os.environ.get("HOME"))

You can also set the default value. When the key exists, the corresponding value is returned, and when the key does not exist, the default value is returned.

print(os.environ.get("HOME", "default"))	#环境变量HOME不存在,返回	default

1.4 Summary of Environment Variable Usage-Setting, Modifying, Obtaining, Deleting, Judging

One, set system environment variables

os.environ['环境变量名称']='环境变量值' #其中key和value均为string类型
os.putenv('环境变量名称', '环境变量值')
os.environ.setdefault('环境变量名称', '环境变量值')

Two, modify the system environment variables

os.environ['环境变量名称']='新环境变量值'

Three, get system environment variables

os.environ['环境变量名称']
os.getenv('环境变量名称')
os.environ.get('环境变量名称', '默认值')	#默认值可给可不给,环境变量不存在返回默认值

Fourth, delete system environment variables

del os.environ['环境变量名称']
del(os.environ['环境变量名称'])

Five, determine whether the system environment variables exist

'环境变量值' in os.environ   # 存在返回 True,不存在返回 False

Guess you like

Origin blog.csdn.net/happyjacob/article/details/109279118