Python实现的本地文件搜索功能示例【测试可用】


Posted in Python onMay 30, 2018

本文实例讲述了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入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
python实现定时同步本机与北京时间的方法
Mar 24 Python
Python实现自动登录百度空间的方法
Jun 10 Python
Python通过future处理并发问题
Oct 17 Python
Pyspider中给爬虫伪造随机请求头的实例
May 07 Python
Python中的CSV文件使用"with"语句的方式详解
Oct 16 Python
一篇文章搞懂Python的类与对象名称空间
Dec 10 Python
Python如何调用外部系统命令
Aug 07 Python
python类的实例化问题解决
Aug 31 Python
Pytorch Tensor的统计属性实例讲解
Dec 30 Python
tensorflow之并行读入数据详解
Feb 05 Python
Python列表操作方法详解
Feb 09 Python
基于python的docx模块处理word和WPS的docx格式文件方式
Feb 13 Python
Pycharm 创建 Django admin 用户名和密码的实例
May 30 #Python
Django使用详解:ORM 的反向查找(related_name)
May 30 #Python
Python实现决策树C4.5算法的示例
May 30 #Python
python实现决策树ID3算法的示例代码
May 30 #Python
浅谈Django中的数据库模型类-models.py(一对一的关系)
May 30 #Python
Python实现的读取电脑硬件信息功能示例
May 30 #Python
Python应用库大全总结
May 30 #Python
You might like
PHP IF ELSE简化/三元一次式的使用
2011/08/22 PHP
simplehtmldom Doc api帮助文档
2012/03/26 PHP
PHP中Enum(枚举)用法实例详解
2015/12/07 PHP
laravel 解决强制跳转 https的问题
2019/10/22 PHP
制作特殊字的脚本
2006/06/26 Javascript
window.location和document.location的区别分析
2008/12/23 Javascript
EasyUI 中 MenuButton 的使用方法
2012/07/14 Javascript
JS 操作Array数组的方法及属性实例解析
2014/01/08 Javascript
AngularJS入门教程之AngularJS模型
2016/04/18 Javascript
Javascript将双字节字符转换成单字节字符并计算长度
2016/06/22 Javascript
jQuery基于BootStrap样式实现无限极地区联动
2016/08/26 Javascript
基于js中的原型(全面讲解)
2017/09/19 Javascript
React Native中的RefreshContorl下拉刷新使用
2017/10/09 Javascript
Js 利用正则表达式和replace函数获取string中所有被匹配到的文本(推荐)
2018/10/28 Javascript
vue打包之后生成一个配置文件修改接口的方法
2018/12/09 Javascript
Smartour 让网页导览变得更简单(推荐)
2019/07/19 Javascript
JavaScript Image对象实现原理实例解析
2020/08/26 Javascript
浅谈javascript事件环微任务和宏任务队列原理
2020/09/12 Javascript
vue-cli中实现响应式布局的方法
2021/03/02 Vue.js
[56:46]2018DOTA2亚洲邀请赛 3.31 小组赛 B组 VP vs Effect
2018/04/01 DOTA
python中使用sys模板和logging模块获取行号和函数名的方法
2014/04/15 Python
python编码最佳实践之总结
2016/02/14 Python
Python二叉搜索树与双向链表转换实现方法
2016/04/29 Python
Python算法之图的遍历
2017/11/16 Python
Python使用pip安装pySerial串口通讯模块
2018/04/20 Python
Python版名片管理系统
2018/11/30 Python
python3实现网络爬虫之BeautifulSoup使用详解
2018/12/19 Python
Python tensorflow实现mnist手写数字识别示例【非卷积与卷积实现】
2019/12/19 Python
Python包,__init__.py功能与用法分析
2020/01/07 Python
python实现删除列表中某个元素的3种方法
2020/01/15 Python
pytorch 模型的train模式与eval模式实例
2020/02/20 Python
法国面料和小百货在线商店:Mondial Tissus
2019/03/23 全球购物
Foot Locker澳洲官网:美国运动服和鞋类零售商
2019/10/11 全球购物
北承题目(C++)
2012/05/16 面试题
Python如何识别银行卡卡号?
2021/06/10 Python
jQuery实现广告显示和隐藏动画
2021/07/04 jQuery