os.environ
The os.environ
is an object that represents users’ environment variables and their values. It gives you a dictionary with the user’s environmental variable names as they keys and their values as the values. The dictionary supports all the operations of the dictionary data structure in Python.
In the code below, we loop through the dictionary returned by the os.environ
. The key is the name, and value is the value of the environment variables:
import osfor name, value in os.environ.items():print("{0}: {1}".format(name, value))
We use the get()
method on the dictionary returned by os.environ
to get the value of a specific environment variable.
import osname = "PATH"print(os.environ.get(name))