Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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 Programming skills with w3resource's quiz.



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m