本文實(shí)例講述了Python3實(shí)現(xiàn)從指定路徑查找文件的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
這里給定一個(gè)搜索路徑,根據(jù)這個(gè)路徑請(qǐng)求和請(qǐng)求的文件名,找到第一個(gè)符合要求的文件
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")
希望本文所述對(duì)大家的Python3程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選