python在电脑硬盘中查找内容的方法,python中查找,python在电脑硬盘中
python在电脑硬盘中查找内容的方法,python中查找,python在电脑硬盘中
python在电脑硬盘中查找内容的方法,是实现需要查找文字内容,可以查目录,也可以查文件里面的内容。运行时间可能比较慢,做为初学python的我,代码写的不够好,希望各位予以指点赐教,这里贴出来分享一下!
#coding:cp936import 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 路径不存在" % pathfor 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 路径不存在" % pathfor 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 pathf.close()breakexcept:pass#www.iplaypy.com#查找当前所有磁盘目录下是否有要找的字符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('XXX')
编橙之家文章,
相关内容
- Python查找指定目录文件glob通配符方法,pythonglob,Python文
- 凯撒移位密码加密算法的Python3.0实现方法,加密算法
- python将色彩HSL转为RGB表示的方法,pythonhsl转为rgb,这是我
- reportlab方法实现中文PDF成生示例,reportlab成生,在使用这
- 通过GDP判断外交关系的练习代码学习和分享,gdp外交关
- 将大数据文件分割为小文件的python方法,文件分割pyt
- ctypes方法调用libcaptcha生成验证码操作,ctypeslibcaptcha,这
- PyQt制作支持拖动缩放小时钟,pyqt拖动缩放,这个用PyQ
- Access密码识别工具py2.7+PyQt实现方法,py2.7pyqt,这是一段
- Python如何使用进程名获取PID方法示例,pythonpid,Python如何
评论关闭