程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python OS environment variables

編輯:Python

One . environment variable :

        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 .
 

Two . Operations related to 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

3、 ... and . Common environment variables key

windows:

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 .

linux:

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved