最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2018年05月30日 09:41:10   作者:shw800  
這篇文章主要介紹了Python實現的本地文件搜索功能,涉及Python針對文件與目錄的遍歷、判斷、編碼轉換、查找等相關操作技巧,需要的朋友可以參考下

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

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

# -*- coding:utf-8 -*-
#! python2
import os
def 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')+"\\"+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程序設計有所幫助。

相關文章

最新評論

旬阳县| 于都县| 墨江| 五指山市| 红安县| 华亭县| 黔西| 凤翔县| 扶沟县| 新干县| 宝坻区| 天峻县| 淮北市| 崇左市| 平山县| 威海市| 综艺| 景洪市| 高碑店市| 晋州市| 固原市| 巴楚县| 久治县| 革吉县| 井陉县| 育儿| 叶城县| 裕民县| 凌海市| 延寿县| 如皋市| 武夷山市| 永定县| 邵武市| 台中市| 泽普县| 广州市| 日土县| 霍州市| 宁南县| 翼城县|