python找指定内容_python查找指定具有相同内容文件的方法
本文实例讲述了python查找指定具有相同内容文件的方法。分享给大家供大家参考。具体如下:
python代码用于查找指定具有相同内容的文件,可以同时指定多个目录
调用方式:python doublesdetector.py c:\;d:\;e:\ > doubles.txt
# Hello, this script is written in Python - http://www.python.org
# doublesdetector.py 1.0p
import os, os.path, string, sys, sha
message = """
doublesdetector.py 1.0p
This script will search for files that are identical
(whatever their name/date/time).
Syntax : python %s
where is a directory or a list of directories
separated by a semicolon (;)
Examples : python %s c:\windows
python %s c:\;d:\;e:\ > doubles.txt
python %s c:\program files > doubles.txt
This script is public domain. Feel free to reuse and tweak it.
The author of this script Sebastien SAUVAGE
http://sebsauvage.net/python/
""" % ((sys.argv[0], )*4)
def fileSHA ( filepath ) :
""" Compute SHA (Secure Hash Algorythm) of a file.
Input : filepath : full path and name of file (eg. 'c:\windows\emm386.exe')
Output : string : contains the hexadecimal representation of the SHA of the file.
returns '0' if file could not be read (file not found, no read rights...)
"""
try:
file = open(filepath,'rb')
digest = sha.new()
data = file.read(65536)
while len(data) != 0:
digest.update(data)
data = file.read(65536)
file.close()
except:
return '0'
else:
return digest.hexdigest()
def detectDoubles( directories ):
fileslist = {}
# Group all files by size (in the fileslist dictionnary)
for directory in directories.split(';'):
directory = os.path.abspath(directory)
sys.stderr.write('Scanning directory '+directory+'...')
os.path.walk(directory,callback,fileslist)
sys.stderr.write('\n')
sys.stderr.write('Comparing files...')
# Remove keys (filesize) in the dictionnary which have only 1 file
for (filesize,listoffiles) in fileslist.items():
if len(listoffiles) == 1:
del fileslist[filesize]
# Now compute SHA of files that have the same size,
# and group files by SHA (in the filessha dictionnary)
filessha = {}
while len(fileslist)>0:
(filesize,listoffiles) = fileslist.popitem()
for filepath in listoffiles:
sys.stderr.write('.')
sha = fileSHA(filepath)
if filessha.has_key(sha):
filessha[sha].append(filepath)
else:
filessha[sha] = [filepath]
if filessha.has_key('0'):
del filessha['0']
# Remove keys (sha) in the dictionnary which have only 1 file
for (sha,listoffiles) in filessha.items():
if len(listoffiles) == 1:
del filessha[sha]
sys.stderr.write('\n')
return filessha
def callback(fileslist,directory,files):
sys.stderr.write('.')
for fileName in files:
filepath = os.path.join(directory,fileName)
if os.path.isfile(filepath):
filesize = os.stat(filepath)[6]
if fileslist.has_key(filesize):
fileslist[filesize].append(filepath)
else:
fileslist[filesize] = [filepath]
if len(sys.argv)>1 :
doubles = detectDoubles(" ".join(sys.argv[1:]))
print 'The following files are identical:'
print '\n'.join(["----\n%s" % '\n'.join(doubles[filesha]) for filesha in doubles.keys()])
print '----'
else:
print message
希望本文所述对大家的Python程序设计有所帮助。
python找指定内容_python查找指定具有相同内容文件的方法相关推荐
- python文件操作2:在指定目录下查找指定后缀名的文件
在指定目录下查找指定后缀名的文件,要查找所有子目录,返回文件路径名列表 import osdef get_full_filelist(base_dir='.', target_ext='') -> ...
- java 文件中查找指定的单词,查找指定文本文件中的单词数的Java程序
查找指定文本文件中的单词数的Java程序 1 说明 在此程序中,我们需要找到给定文本文件中出现次数最多的单词.这可以通过使用文件指针以读取模式打开文件来完成.逐行读取文件.一次分割一行并存储在数组中. ...
- python 找到两个排序数组的中位数_Python查找两个有序列表中位数的方法【基于归并算法】...
本文实例讲述了Python查找两个有序列表中位数的方法.,具体如下: 今天做到的一个机试题目,很简单,这里简单记录一下: 我用的是归并的思想,当然还可以用递归的方法,下面是具体实现: #!usr/bi ...
- python替换 输入手机号_python file模块 替换输入内容脚本
root@python-10:/home/liujianzuo/python/test# ls passwd rc.local test1 root@python-10:/home/liujianzu ...
- python找电影资源_Python爬虫爬取BT之家找电影资源
head.jpg 一.写在前面 最近看新闻说圣城家园(SCG)倒了,之前BT天堂倒了,暴风影音也不行了,可以说看个电影越来越费力,国内大厂如企鹅和爱奇艺最近也出现一些幺蛾子,虽然目前版权意识虽然越来越 ...
- python xlwt追加内容_python 往excel中追加内容
from xlrd import open_workbook from xlutils.copy import copy #在创建好的excel种追加新的数据 if __name__ == '__ma ...
- python批量分析表格_python批量设置多个Excel文件页眉页脚的脚本
python批量设置多个Excel文件页眉页脚的脚本 本文实例为大家分享了python批量设置多个Excel文件页眉页脚的具体代码,供大家参考,具体内容如下 import os import open ...
- python当前运行目录_Python获取运行目录与当前脚本目录的方法
Python获取运行目录与当前脚本目录的方法 本文实例讲述了Python获取运行目录与当前脚本目录的方法.分享给大家供大家参考.具体实现方法如下: import os import sys #运行目录 ...
- python中docx模块的使用_python使用docx模块读写docx文件的方法与docx模块常用方法详解...
一,docx模块 Python可以利用python-docx模块处理word文档,处理方式是面向对象的.也就是说python-docx模块会把word文档,文档中的段落.文本.字体等都看做对象,对对象 ...
最新文章
- 2021-春季学习-智能车技术创新与实践(90)
- 经典JavaScript正则表达式实战
- mascara-2(MetaMask/mascara本地实现)-连接线上钱包
- React.js 的 Web 应用场景有哪些
- 使用centos7构建本地git服务器
- hdu 5441 (并查集)
- matlab dynprog,动态规划算法
- Win10:ssh报错:RSA host key for 192.168.3.10 has changed and you have requested strict checking.
- 给你的应用“一只”智慧的眼睛 —— Barcode常识普及以及识别信息处理
- Atitit.研发团队与公司绩效管理的原理概论的attilax总结
- spss相关性分析_关公配赤兔——因子分析和SPSS
- OpenEmu for mac(苹果系统原生游戏模拟软件)中文版
- 新加坡国立大学计算机学院师资,我系教师刘志刚赴新加坡国立大学访学
- openid与商户appid不匹配
- POI excel添加数据有效性验证以及解决DataValidation超长的问题
- 物联网 PaaS 平台大盘点
- (四)Vue之数据绑定
- CSS3 3D转换和旋转木马案例
- 判断程序是否正在运行
- 卫星运行的规律与卫星定位原理