Python os.get_exec_path() Method

Python os.get_exec_path() Method

  1. Syntax of the os.get_exec_path() Method
  2. Example Codes: Working With the os.get_exec_path() Method in Python
  3. Example Codes: Use an environment Parameter in the os.get_exec_path() Method in Python
  4. Example Codes: Use the for Loop in the os.get_exec_path() Method in Python

Python os.get_exec_path() method is an efficient way of extracting a list of required directories. While launching any process, the directories will be searched for the specified executable.

Syntax of the os.get_exec_path() Method

os.get_exec_path(environment = None)

Parameters

environment = None Optional. The default value is None; when the environment is set to None, the environ environment is used. This parameter is a dictionary of environment variables offered by Python.

Return

The return type of this method is a list of all the paths searched for the specified executable while launching a process.

Example Codes: Working With the os.get_exec_path() Method in Python

import os

print("The following paths will be searched for the named executable:")

print(os.get_exec_path())

Output:

The following paths will be searched for the named executable:
['/opt/swift/swift-5.0-RELEASE-ubuntu14.04/usr/bin/', '/usr/local/sbin', '/usr/local/bin', '/usr/sbin', '/usr/bin', '/sbin', '/bin']

No parameter is entered in the above code, so the default environment environ is used. The environ is a mapping object that contains all the user’s environmental variables.

Example Codes: Use an environment Parameter in the os.get_exec_path() Method in Python

import os

environment = {'HOME': '/home/user'}

print("The following paths will be searched for the named executable:")

print(os.get_exec_path(environment))

Output:

The following paths will be searched for the named executable:
['/bin', '/usr/bin']

When a system-related error occurs while using this method, then an OSError is thrown.

Example Codes: Use the for Loop in the os.get_exec_path() Method in Python

import os

directories = os.get_exec_path()

print("The following paths will be searched for the named executable:")

for path in directories:

    print(path)

Output:

The following paths will be searched for the named executable:
/opt/swift/swift-5.0-RELEASE-ubuntu14.04/usr/bin/
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin

The os.get_exec_path() method uses a set of environment variables available to Python through the os.environ of the OS module.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method