Environment variables are the means of communication between programs and operating systems . Some characters should not be written in plaintext , For example, database password , Personal account password , If you write it into your local environment variable , When the program is used, it passes os.environ.get() Just take it out .os.environ Is a dictionary of environment variables .
import os
"""
Set up / modify environment variable :
os.environ[‘ Environment variable name ’]=‘ Environment variable value ’ # among key and value Are all string type ;
Two ways :
"""
os.environ["test01"]="01"
os.environ.setdefault('test02', '02')
"""
Query environment variables :
os.environ Is a dictionary of environment variables ; May, in accordance with the Operate in the way of dictionary
"""
if "test01" in os.environ:
print('exist')
else:
print('not exist')
# exist
print(type(os.environ))
# <class 'os._Environ'>
for k, v in os.environ.items():
print(k , '--', v)
# TEST01 - - 01
# TEST02 - - 02
"""
Get environment variables :
"""
print(os.environ['test01'])
print(os.environ.get('test01'))
print(os.getenv("test01"))
# You can also set the default value , Returns the corresponding value when the key exists , When there is no , Return default
print(os.environ.get("HOME", "default")) # environment variable HOME non-existent , return default
"""
Delete environment variables
"""
del(os.environ["test01"])
if "test01" in os.environ:
print('exist')
else:
print('not exist')
# not exist
os.environ['HOMEPATH']: Current user home directory .
os.environ['TEMP']: Temporary directory path .
os.environ["PATHEXT"]: Executable file .
os.environ['SYSTEMROOT']: System home directory .
os.environ['LOGONSERVER']: machine name .
os.environ['PROMPT']: Set prompt .os.environ['USERNAME']: Current user .
os.environ['USER']: Current user .
os.environ['LC_COLLATE']: The alphabetical order when sorting the results of path extension .
os.environ['SHELL']: Use shell The type of .
os.environ['LAN']: The language used .
os.environ['SSH_AUTH_SOCK']:ssh Execution path of .