Last active
May 2, 2023 09:28
-
-
Save FerusAndBeyond/952394b650befaf51d6a8471e9f4bd63 to your computer and use it in GitHub Desktop.
env-variable part 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
# first way | |
os.environ['TERM_PROGRAM'] | |
# => 'Apple_Terminal' | |
# a non-existing variable will lead to error | |
os.environ["THIS_VARIABLE_DOESNT_EXIST"] | |
# => KeyError: 'THIS_VARIABLE_DOESNT_EXIST' | |
# second way | |
print(os.getenv("TERM_PROGRAM")) | |
# => 'Apple_Terminal' | |
# a non-existing variable will lead to None | |
print(os.getenv("THIS_VARIABLE_DOESNT_EXIST")) | |
# => None | |
# but you could also specify if you want it to return | |
# something else if the key does not exist | |
print(os.getenv("THIS_VARIABLE_DOESNT_EXIST", "Return this if it does not exist...")) | |
# => "Return this if it does not exist..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment