Python OS Module: Accessing Envioronment Variables














































Python OS Module: Accessing Envioronment Variables



Python OS Module: Accessing Envioronment Variables


 Accessing environment variables
   
    1.os.getenv(key,default) 

    2.os.getenvb() 

    3. os.getlogin() 

    4. os.putenv() and os.environ()
    

       
1.os.getenv(key,default) 
 
os.getenv() method in Python returns the value of the environment variable key if it exists otherwise returns the default value.

Syntax: os.getenv(key, default = None)

Parameters:

key: string denoting the name of environment variable
default (optional) : string denoting the default value in case key does not exists. If omitted default is set to %u2018None%u2019.

Return Type: This method returns a string that denotes the value of the environment variable key. In case key does not exists it returns the value of default parameter.

Type the following code in your IDE:

>>>import os >>>key=%u201DHOME%u201D >>>os.getenv(key)
  

Output:


>>> key="osfile.txt" >>> value=os.getenv(key) >>> print(value)

Output:


2.os.getenvb() 
       
os.getenvb(key) return the value of the environment variable key if it exists, or default if it doesn%u2019t. key, default and the result are bytes.getenvb() is only available if supports_bytes_environ is True.


3. os.getlogin() 
       
os.getlogin() method in Python is used to get the name of the user logged in on the controlling terminal of the process.

Syntax: os.getlogin()

Return Type: This method returns a string that denotes the name of the user logged in on the controlling terminal of the process.

Type the following code in your Python IDE:


>>> import os >>> user=os.getlogin() 
>>> print(user)Output:



4. os.putenv() and os.environ()

The os.environ value is known as a mapping object that returns a dictionary of the user%u2019s environmental variables. You may not know this, but every time you use your computer, some environment variables are set. These can give you valuable information, such as number of processors, type of CPU, the computer name, etc. Let%u2019s see what we can find out about our machine:

>>> import os
>>> os.environ



Your output will be different from my output, but you%u2019ll see something similar. As you may have noticed, this returned a dictionary. That means you can access the environmental variables using your normal dictionary methods. 

You could also use the os.getenv function to access this environmental variable. This is because os.envioron returns a dictionary.

>>> os.getenv('TERM')


The benefit of using os.getenv() instead of the os.environ dictionary is that if we happen to try to access an environmental variable that doesn%u2019t exist, the getenv function will just return None. If you did the same thing with os.environ, you would receive an error. Let%u2019s give it a try so you can see what happens:

>>> os.getenv('TERM2') >>> os.environ['TERM2']

Output:


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/os.py", line 669, in __getitem__
    raise KeyError(key) from None

KeyError: 'TERM2'

      

Thanks for viewing my article! Please like and comment!


Comments