您的位置:首页 > 编程语言 > Python开发

从指定的搜索路径寻找文件

2015-12-28 22:41 716 查看
#-*-coding:utf-8-*-
'''
Created on 2015年12月28日

@author: Zroad
'''

import os

def search_file(filename, search_path, pathsep=os.pathsep):
"""
给定搜索路径,根据请求的名字找到文件
"""
for path in search_path.split(pathsep):
candidate = os.path.join(path, filename)
if os.path.isfile(candidate):
return os.path.abspath(candidate)
return None

if __name__ == "__main__":
search_path = "E:\\PythonTest" + os.pathsep + "E:\\src"
find_file = search_file("Log4JTest.jar",search_path) #E:\src\Log4JTest.jar
if find_file:
print "File 'Log4JTest.jar' found at %s"  % find_file
else:
print "File 'Log4JTest.jar' not found"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 搜索文件