Python | os.getenv() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
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 ‘None’.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.
Code #1: use of os.getenv() method
# Python program to explain os.getenv() method # importing os module import os # Get the value of 'HOME' # environment variable key = 'HOME'value = os.getenv(key) # Print the value of 'HOME' # environment variable print("Value of 'HOME' environment variable :", value) # Get the value of 'JAVA_HOME' # environment variable key = 'JAVA_HOME'value = os.getenv(key) # Print the value of 'JAVA_HOME' # environment variable print("Value of 'JAVA_HOME' environment variable :", value) |
Value of 'HOME' environment variable : /home/ihritik Value of 'JAVA_HOME' environment variable : /opt/jdk-10.0.1
Code #2: if key does not exist
# Python program to explain os.getenv() method # importing os module import os # Get the value of 'home' # environment variable key = 'home'value = os.getenv(key) # Print the value of 'home' # environment variable print("Value of 'home' environment variable :", value) |
Value of 'home' environment variable : None
Code #3: Explicitly specifying default parameter
# Python program to explain os.getenv() method # importing os module import os # Get the value of 'home' # environment variable key = 'home'value = os.getenv(key, "value does not exist") # Print the value of 'home' # environment variable print("Value of 'home' environment variable :", value) |
Value of 'home' environment variable : value does not exist
Recommended Posts:
- class method vs static method in Python
- Python | next() method
- Python | os.dup() method
- Python | set() method
- Python | os._exit() method
- Python | os.WEXITSTATUS() method
- Python | os.set_inheritable() method
- Python | os.cpu_count() method
- Python | Decimal min() method
- Python | sympy.tan() method
- Python | sympy.sin() method
- Python | sympy.cos() method
- Python | PyTorch cos() method
- Python | Decimal max() method
- Python | range() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




