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

本文实例讲述了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程序设计有所帮助。

Posted in 未分类

发表评论