本文實例講述了Python3實現(xiàn)從指定路徑查找文件的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
這里給定一個搜索路徑,根據(jù)這個路徑請求和請求的文件名,找到第一個符合要求的文件
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import os def search_file(file_name, search_path, pathsep = os.pathsep): for path in search_path.split(pathsep): candidate = os.path.join(path, file_name) if os.path.isfile(candidate): return os.path.abspath(candidate) return None search_path = 'd:\\pm\\pm' find_file = search_file( 'babyos.img' , search_path) if find_file: print ( "File 'babyos.img' found at %s" % find_file) else : print ( "File 'babyos.img' not found" ) |
希望本文所述對大家的Python3程序設(shè)計有所幫助。