zoukankan      html  css  js  c++  java
  • os.environ

    Syntax: os.environ
    
    Parameter: It is a non-callable object. Hence, no parameter is required
    
    Return Type: This returns a dictionary representing the user’s environmental variables

    os.environ in Python is a mapping object that represents the user’s environmental variables. It returns a dictionary having user’s environmental variable as key and their values as value.

    # Python program to explain os.environ object 
    
    # importing os module 
    import os 
    import pprint 
    
    # Get the list of user's 
    # environment variables 
    env_var = os.environ 
    
    # Print the list of user's 
    # environment variables 
    print("User's Environment variable:") 
    pprint.pprint(dict(env_var), width = 1) 

    os.environ behaves like a python dictionary, so all the common dictionary operations like get and set can be performed. We can also modify os.environ but any changes will be effective only for the current process where it was assigned and it will not change the value permanently.

    # Python program to explain os.environ object 
    
    
    # importing os module 
    import os 
    
    # Method 1 
    # Print the value of 
    # 'MY_HOME' environment variable 
    print("MY_HOME:", os.environ.get('MY_HOME', "Environment variable does not exist")) 
    
    
    # Method 2 
    try: 
        print("MY_HOME:", os.environ['MY_HOME']) 
    except KeyError: 
        print("Environment variable does not exist") 
  • 相关阅读:
    HDU-2222 Keywords Search(AC自动机)
    HDU-2647 Reward(拓扑排序)
    HDU-2896 病毒侵袭(AC自动机)
    UESTC-1057 秋实大哥与花(线段树+成段加减+区间求和)
    CSU-1120 病毒(最长递增公共子序列)
    记忆化搜索
    区间动态规划 矩阵连乘 Medium
    34枚金币时间管理法
    摄影基础1
    学习法则 讲
  • 原文地址:https://www.cnblogs.com/Mint-diary/p/13272973.html
Copyright © 2011-2022 走看看