判断给定路径的文件是否存在
import os
def check_existence(path_of_file):
path_of_file = os.path.abspath(path_of_file)
return os.path.exists(path_of_file)
在给定目录列表中寻找指定名称,指定类型的文件
# 代码源自openstack帮助库,oslo.config.cfg.py模块
import os
def _search_dirs(dirs, basename, extension=""):
"""Search a list of directories for a given filename.
Iterator over the supplied directories, returning the first file
found with the supplied name and extension.
:param dirs: a list of directories
:param basename: the filename, for example 'glance-api'
:param extension: the file extension, for example '.conf'
:returns: the path to a matching file, or None
"""
for d in dirs:
path = os.path.join(d, '%s%s' % (basename, extension))
if os.path.exists(path):
return path