📜  Python| os.get_exec_path() 方法

📅  最后修改于: 2022-05-13 01:55:12.010000             🧑  作者: Mango

Python| os.get_exec_path() 方法

Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。

os 模块中的所有函数在文件名和路径无效或不可访问的情况下,或具有正确类型但操作系统不接受的其他参数的情况下引发OSError

Python中的os.get_exec_path()方法用于获取在启动进程时将搜索命名可执行文件的目录列表。

代码 #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:指定 env 参数

# 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']