Last active
August 29, 2015 13:58
-
-
Save maximd/10382823 to your computer and use it in GitHub Desktop.
Print environment variables in python
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
#!/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