Skip to content

Instantly share code, notes, and snippets.

@maximd
Last active August 29, 2015 13:58
Show Gist options
  • Save maximd/10382823 to your computer and use it in GitHub Desktop.
Save maximd/10382823 to your computer and use it in GitHub Desktop.
Print environment variables in python
#!/usr/bin/env python
"""Print environment variables."""
import os
# print environment variables
for k in os.environ:
print('%s: %s' % (k, os.environ[k]))
# print environment variables with alphabetical sort
for k in sorted(os.environ):
print('%s: %s' % (k, os.environ[k]))
# sorted alternative (from http://stackoverflow.com/a/7995743/576371)
for name, value in sorted(os.environ.items()):
print('%s: %s' % (name, value))
# sorted oneliner (from http://stackoverflow.com/a/7995998/576371)
print('\n'.join('%s: %s' % x for x in sorted(os.environ.items())))
# sorted oneliner alternative
[(k, os.environ[k]) for k in sorted(os.environ)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment