find_file

(函数来自 pyomo.common.fileutils)

pyomo.common.fileutils.find_file(filename, cwd=True, mode=4, ext=None, pathlist=[], allow_pathlist_deep_references=True)[source]

根据一组搜索参数定位文件

Parameters:
  • filename (str) – 要定位的文件名。文件名可能包含对用户主目录的引用 (~user)、环境变量 (${HOME}/bin) 和 shell 通配符 (?*);所有这些都将被展开。

  • cwd (bool) – 首先在当前工作目录中查找 [默认值: True]

  • mode (mask) – 如果不为None,则只返回可以读取/写入/执行的文件。有效值是{os.R_OK, os.W_OK, os.X_OK}的包含OR [默认: os.R_OK]

  • ext (striterablestr) – 如果不为 None,也会查找 filename+ext [默认值: None]

  • pathlist (striterablestr) – 包含要搜索路径的字符串列表,每个字符串包含一个路径。如果 pathlist 是一个字符串,则首先使用 os.pathsep 进行分割以生成 pathlist [默认值: []]。

  • allow_pathlist_deep_references (bool) – 如果 allow_pathlist_deep_references 为 True 且文件名看起来是相对路径,则允许相对于 pathlist 中的目录进行深度引用匹配(例如,如果文件名是 foo/my.exe/usr/bin 在 pathlist 中,则 find_file() 可能返回 /usr/bin/foo/my.exe)。如果 allow_pathlist_deep_references 为 False 且文件名看起来是相对路径,则只允许相对于当前目录的匹配(假设 cwd==True)。[默认值: True]

注释

find_file 使用 glob,因此路径和/或文件名可能包含通配符。返回第一个匹配的文件。