For a python program, I am using os.environ()
to get environment variables with a certain key. However, I would like to be able do something if the key is nonexistent in the environment, instead of throwing a KeyError
.
I've looked up a few solutions, but os.environ.get()
and os.getenv()
seem to act differently than how I expect. Is there any way to use os.environ()
and still work around nonexistent environment variables?
os.environ.get("foo")
returns None
if "foo" is not found.
You can also use
if "foo" in os.environ:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With