Python实现查找系统盘中需要找的字符

作者:G0561 时间:2022-10-02 02:35:50 

本文实例讲述了Python实现查找系统盘中需要找的字符。分享给大家供大家参考。具体如下:


'''
Created on 2011-7-13
@author: 123
'''
import os
#保存当前有的磁盘
def existdisk():
curdisks = []
allDisks = ['C:', 'D:', 'E:', 'F:', 'G:', 'H:', 'I:', 'J:', 'K:', \
   'L:', 'M:', 'N:', 'O:', 'P:', 'Q:', 'R:', 'S:', 'T:', \
   'U:', 'V:', 'W:', 'X:', 'Y:', 'Z:', 'A:', 'B:']
for disk in allDisks:
 if os.path.exists(disk):
  curdisks.append(disk)
return curdisks
#目录中含有查找的字符
def SearchDirFile(path, src):
if not os.path.exists(path):
 print "%s 路径不存在" % path
for root , dirs, files in os.walk(path, True):
 if - 1 != root.find(src):
 #路径名中是否存在要查找的字符
  print root
 for item in files:
   path = os.path.join(root, item)
   if - 1 != path.find(src):
   #文件列表中是否有要查找的字符
   print path
#查找文件内容中有要查找的字符
def SearchFile(path, src):
if not os.path.exists(path):
 print "%s 路径不存在" % path
for root, dirs, files in os.walk(path, True):
 for item in files:
  path = os.path.join(root, item)
  try:
   f = open(path, 'r')
   for eachline in f.readlines():
    if - 1 != eachline.find(src):
    #文本内容中是否有要查找的字符
     print path
     f.close()
     break
  except:
   pass
#查找当前所有磁盘目录下是否有要找的字符
def SearchAllDirFile(src):
curdisks = existdisk()
for disk in curdisks:
 disk = disk + '\\'
 SearchDirFile(disk, src)
print "完成搜索"
#查找当前所有磁盘目录文件内容下是否有要找的字符
def SearchALLFile(src):  
curdisks = existdisk()
for disk in curdisks:
 disk = disk + "\\"
 SearchFile(disk, src)
print "完成搜索"
SearchALLFile('十进制转二进制')

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

标签:Python,查找
0
投稿

猜你喜欢

  • python实现机器人卡牌

    2023-05-29 17:27:38
  • Python中获取网页状态码的两个方法

    2023-08-27 22:47:21
  • web标准知识——美化段落文本 Ⅱ

    2008-07-22 12:12:00
  • Python下调用Linux的Shell命令的方法

    2023-08-10 01:43:17
  • 将数组中指定数量的元素移动数组后面的实现代码

    2023-11-16 22:07:56
  • Facebook基础的信息架构图

    2008-04-01 09:46:00
  • [译]艺术和设计的差异 (1)

    2009-09-25 12:38:00
  • WEB2.0网页制作标准教程(12)XHTML校验及常见错误

    2008-02-19 19:59:00
  • 10条影响CSS渲染速度的写法与建议

    2008-09-09 22:02:00
  • 对网站内嵌gradio应用的输入输出做审核实现详解

    2023-07-22 08:22:05
  • 让MYSQL彻底支持中文

    2008-12-24 16:23:00
  • 使用Python从有道词典网页获取单词翻译

    2022-12-10 21:18:03
  • Python常用小技巧总结

    2023-02-27 17:50:16
  • 教你怎么用Python处理excel实现自动化办公

    2023-11-05 02:19:50
  • 也谈用户体验

    2009-07-15 12:56:00
  • 小看了setTimeout()

    2009-12-04 12:44:00
  • javascript面向对象技术基础(四)

    2010-02-07 13:15:00
  • 用画为5.12地震受灾同胞们祈福

    2008-05-20 12:08:00
  • 解析:快速的掌握 MySQL支持的操作系统

    2008-12-31 17:18:00
  • 解剖JavaScript中的null和undefined

    2009-03-01 12:49:00
  • asp之家 网络编程 m.aspxhome.com