国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現的本地文件搜索功能示例【測試可用】

2020-02-15 21:34:41
字體:
來源:轉載
供稿:網友

本文實例講述了Python實現的本地文件搜索功能。分享給大家供大家參考,具體如下:

偶爾需要搜索指定文件,不想每次都在windows下面去搜索,想用代碼來實現搜索,而且能夠收集搜索結果,于是有了下面的代碼。

# -*- coding:utf-8 -*-#! python2import osdef search_file(fileNmae, path):  '''search a file in target directory  :param fileNmae: file to be searched  :param path: search scope  :return:file list  '''  flag = False  count = 0  result_list = []  if os.path.exists(path):    for root, dirs, files in os.walk(path):      for fn in files:        Name = fn.decode('gbk')        cu_path = root.encode('gbk')+"http://"+Name        if Name.lower().find(fileNmae.lower()) != -1 and os.path.isfile(cu_path):          print ":::Find it,file no", count+1, ":", cu_path          flag = True          count += 1          result_list.append(cu_path)    if flag is False:      print ":::Not found the file:", fileNmae, "in path:", path    else:      print "======== Get[", count, "]files ========"      return result_list  else:    print "!!-----path not existed:", path#測試:search_file("4.jpg", "C://img")

運行結果:

更多Python相關內容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python編碼操作技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程》

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄂托克前旗| 姚安县| 信丰县| 万宁市| 华安县| 铜川市| 腾冲县| 海林市| 麻阳| 垫江县| 横峰县| 上虞市| 阿坝县| 自贡市| 招远市| 侯马市| 西乌珠穆沁旗| 太和县| 扎鲁特旗| 密云县| 武功县| 九龙县| 肥西县| 金川县| 乌恰县| 福安市| 自治县| 乐都县| 香港| 绩溪县| 田东县| 安宁市| 桃园市| 工布江达县| 宣武区| 商城县| 腾冲县| 湄潭县| 米泉市| 桑日县| 手游|