Python: Access environment variables and value of the environment variable
Python Operating System Services: Exercise-10 with Solution
Write a python program to access environment variables and value of the environment variable.
Sample Solution:
Python Code :
import os
print("Access all environment variables:")
print('*----------------------------------*')
print(os.environ)
print('*----------------------------------*')
print("Access a particular environment variable:")
print(os.environ['HOME'])
print('*----------------------------------*')
print(os.environ['PATH'])
print('*----------------------------------*')
print('Value of the environment variable key:')
print(os.getenv('JAVA_HOME'))
print(os.getenv('PYTHONPATH'))
Sample Output:
Access all environment variables: *----------------------------------* environ({'kill_retry_time': '100', 'windowsHide': 'true', 'username': 'root', 'treekill': 'true', 'automation': 'true', 'pmx': 'true', 'instance_var': 'NODE_APP_INSTANCE',
'autorestart': 'true', 'vizion': 'true', 'merge_logs': 'true', 'env': '[object Object]', 'name': 'shell', 'node_args': '', 'pm_exec_path':
'/trinket-workdir/server/python3/shell.js', 'pm_cwd': '/trinket-workdir', 'exec_interpreter': 'node', 'instances': '1', 'exec_mode': 'fork_mode', 'pm_out_log_path': '/dev/null',
'pm_err_log_path': '/dev/null', 'pm_pid_path': '/root/.pm2/pids/shell-0.pid', 'km_link': 'false', 'vizion_running': 'false', 'NODE_APP_INSTANCE': '0', 'PATH':
'/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', 'HOSTNAME': '0c299cb8f897', 'DEBIAN_FRONTEND': 'teletype', 'LANG': 'en_US.UTF-8', 'LANGUAGE': 'en_US:en', 'LC_ALL':
'en_US.UTF-8', 'HOME': '/root', 'TERM': 'xterm', 'MPLBACKEND': 'module://trinket_backend', 'PYTHONPATH': '/trinket/python3', 'PM2_PROGRAMMATIC': 'true', 'PM2_DISCRETE_MODE':
'true', 'PM2_INTERACTOR_PROCESSING': 'true', 'PM2_HOME': '/root/.pm2', 'unique_id': 'e2361fcd-b526-4ac0-84dd-d76bff99835e', 'status': 'launching', 'pm_uptime': '1579199831536',
'axm_actions': '', 'axm_monitor': '[object Object]', 'axm_options': '[object Object]', 'axm_dynamic': '[object Object]', 'created_at': '1578771931859', 'pm_id': '0',
'restart_time': '578', 'unstable_restarts': '0', 'version': '0.0.0', 'versioning': 'null', 'node_version': '8.11.3', 'exit_code': '1', 'prev_restart_delay': '0'})
*----------------------------------* Access a particular environment variable: /root *----------------------------------* /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin *----------------------------------* Value of the environment variable key: None /trinket/python3
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to retrieve the current working directory and change the dir (moving up one).
Next: Write a Python program to iterate over a root level path and print all its sub-directories and files, also loop over specified dirs and files.
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
Python: Unknown Arguments Using *arguments
If your function can take in any number of arguments then add a * in front of the parameter name:
def myfunc(*arguments): for a in arguments: print a myfunc(a) myfunc(a,b) myfunc(a,b,c)
- New Content published on w3resource :
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework