Python os.get_exec_path()
os模块中的所有函数在文件名和路径无效或不可访问,或其他具有正确类型但操作系统不接受的参数时都会引发OSError。
Python中的os.get_exec_path()方法用于获取在启动进程时将被搜索到的指定可执行文件的目录列表。
语法:os.get_exec_path(env =None)
参数:
env(可选):表示环境变量的字典。这个参数的默认值是None。如果其值为None,则使用环境。
返回类型:该方法返回一个列表,该列表表示在启动进程时将用于搜索指定的可执行文件的目录路径。
示例1
使用os.get_exec_path()方法
# Python program to explain os.get_exec_path() method
# importing os module
import os
# Get the list of directories
# that will be used to search
# a named executable
# while launching a process
exec_path = os.get_exec_path()
# Print the list
print("Following paths will be searched for a named executable:")
print(exec_path)
输出:
Following paths will be searched for a named executable:
['/usr/local/sbin', '/usr/local/bin', '/usr/sbin', '/usr/bin', '/sbin', '/bin', '/usr/games', '/usr/local/games', '/snap/bin', '/usr/local/java/jdk-10.0.1/bin', '/usr/local/java/jdk-10.0.1/jre/bin', '/opt/jdk-10.0.1/bin', '/opt/jdk-10.0.1/jre/bin']
示例2
指定环境参数
# Python program to explain os.get_exec_path() method
# importing os module
import os
# Dictionary of environment variable
env = {'HOME': '/home/ihritik'}
# Get the list of directories
# that will be used to search
# a named executable
# while launching a process
exec_path = os.get_exec_path(env)
# Print the list
print("Following paths will be searched for a named executable:")
print(exec_path)
输出:
Following paths will be searched for a named executable:
['', '/bin', '/usr/bin']