How can os.environ be used in Python?
In Python, os.environ is a dictionary object that is used to access and modify environment variables. It contains all of the environment variables for the current process.
To obtain the value of an environment variable, you can use the os.environ dictionary object by indexing. For example, to obtain the value of the environment variable named “PATH”, you can use the following code:
import os
path = os.environ["PATH"]
print(path)
To set the value of an environment variable, you can directly modify the os.environ dictionary object. For example, to set an environment variable named “MY_VAR” to “my_value”, you can use the following code:
import os
os.environ["MY_VAR"] = "my_value"
Please note that the modified environment variables only take effect in the current process and do not affect other processes or system environment variables.
To remove an environmental variable, you can use the `del` statement to delete the corresponding entry in the `os.environ` dictionary object. For example, to delete an environmental variable named “MY_VAR”, you can use the following code:
import os
del os.environ["MY_VAR"]
Note that the deleted environment variables are only removed within the current process and do not have any impact on other processes or system environment variables.