| by Arround The Web | No comments

Python OS Environ

The “Environment Variables” are a key-value pairs set that are utilized to keep the information of the current environment. To add, get, remove, or modify the environment variable, the “os.environ” mapping object of the “os” module is used in Python.

In this Python tutorial, we will present a detailed guide on Python “os.environ” using numerous examples and via the following content:

What is the “os.environ” in Python?

In Python, the “os.environ” is utilized to represent all the environment variables of the present/current process as a dictionary. In Python, the “os.environ” is a mapping object that behaves/acts like a dictionary. It can be used to get and set environment variables and store things like the user’s home directory, the path to the executable file, the current working directory, and others.

Let’s understand this via the below examples:

Example 1: Accessing Environment Variables Using “os.environ”

The below example code is used to access all environment variables using the “os.environ” object:

import os, pprint
print("User's Environment variable:")
pprint.pprint(dict(os.environ), width = 1)

 

In the above code, the “os.environ” mapping object creates/constructs a dictionary-like object, including all the environment variables that are presently defined. The “pprint.pprint()” is used to pretty print all the user environment variables values.

Output

Example 2: Accessing Specified/Particular Environment Variable Using “os.environ”

The below code is used to access the particular environment variables key value:

import os
print("Environment Variable Value:", os.environ['HOMEPATH'])

 

Here, the “os.environ[]” takes the specified environment variable key name and retrieves the value.

Output

Example 3: Adding New Environment Variable Using “os.environ”

To add or set a new environment variable, take the below example code:

import os
os.environ['Linuxhint'] = 'Python Guide'
print("Linuxhint:", os.environ['Linuxhint'])

 

In this code, the “os.environ” object adds an environment variable key called “Linuxhint” to the value “Python Guide”. After setting the specific environment variable, we access or call it using its key name.

Output

Example 4: Modifying Environment Variables Using “os.environ”

To modify the environment variable, the following example is used in Python:

import os
os.environ['Linuxhint'] = 'Python Guide'
print("Original Environment Variable Value:", os.environ['Linuxhint'])
os.environ['Linuxhint'] = 'Welcome to Linux Guide'
print("\nModified Environment Variable Value:", os.environ['Linuxhint'])

 

In the above code, the “os.environ” object is used to add/set the specified environment variable by specifying the key-value pair. After adding, we modified the environment variable by reassigning a new value to the key:

Output

Example 5: Accessing and Handling Error of Environment Variable Which Does Not Exist Using “os.environ”

The “os.environ” object can retrieve a “Key-Error” if the particular environment variable does not exist. Here is an example code:

import os
try:
    print("Linuxhint:", os.environ['Linuxhint'])
except:
    print("Environment Variable Does Not Exist")

 

In the above code, the try block executes and retrieves the environment/users variable value using the “os.environ” object if the environment/user variable exists. But If the environment variable does not exist, the except block will execute and display the exception message.

Output

That’s all about the Python os.environ object.

Conclusion

The “os.environ” is a mapping object or built-in dictionary in Python that represents all the user’s environment variable values in key-value pairs. It can be used to access all or specified environment variables, set new environment variables, and modify environment variables in Python. This Python tutorial has delivered a comprehensive guide on Python “os.environ” objects using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply