现在的位置: 首页 > 综合 > 正文

002_020 Python 在Python的搜索路径中寻找文件

2018年02月14日 ⁄ 综合 ⁄ 共 525字 ⁄ 字号 评论关闭

代码如下:

#encoding=utf-8

print '中国'

#在Python的搜索路径中寻找文件

import sys,os

class Error(Exception):pass

def _find(pathname, matchFunc=os.path.isfile):
    for dirname in sys.path:
        candidate = os.path.join(dirname, pathname)
        print candidate
        if matchFunc(candidate):
            return candidate
    raise Error("Can't find it")

def findFile(pathname):
    return _find(pathname)
def findDir(path):
    return _find(path,os.path.isdir)

print findFile(r'_bsddb.pyd')

打印结果如下:

中国
F:\workspace\StudyPy\src\basic\_bsddb.pyd
F:\workspace\StudyPy\src\_bsddb.pyd
D:\Python27\DLLs\_bsddb.pyd
D:\Python27\DLLs\_bsddb.pyd

抱歉!评论已关闭.