把开发过程中经常用的内容珍藏起来,下边资料是关于python用于查找指定具有相同内容的文件的代码,应该能对码农有较大好处。

# doublesdetector.py 1.0p
import os, os.path, string, sys, shamessage = """
doublesdetector.py 1.0pThis script will search for files that are identical
(whatever their name/date/time).Syntax : python %s <directories>where <directories> is a directory or a list of directoriesseparated by a semicolon (;)Examples : python %s c:windowspython %s c:;d:;e: > doubles.txtpython %s c:program files > doubles.txtThis script is public domain. Feel free to reuse and tweak it.
The author of this script Sebastien SAUVAGE <sebsauvage at sebsauvage dot net>def fileSHA ( filepath ) :""" Compute SHA (Secure Hash Algorythm) of a file.Input : filepath : full path and name of file (eg. 'c:windowsemm386.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 filefor (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 filefor (sha,listoffiles) in filessha.items():if len(listoffiles) == 1:del filessha[sha]sys.stderr.write('n')return filesshadef 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      复制代码

转载于:https://juejin.im/post/5cd8b7c3e51d453a69177ec7

python源码用于查找指定具有相同内容的文件相关推荐

  1. python找指定内容_python查找指定具有相同内容文件的方法

    本文实例讲述了python查找指定具有相同内容文件的方法.分享给大家供大家参考.具体如下: python代码用于查找指定具有相同内容的文件,可以同时指定多个目录 调用方式:python doubles ...

  2. python源码只有编译成二进制_【转】Python源代码编译成 pyc pyo

    原文地址 http://blog.csdn.net/sislcb/archive/2009/03/18/4002414.aspx什么是pyc文件 pyc是一种二进制文件,是由py文件经过编译后,生成的 ...

  3. 分享57个Python源码,总有一款适合您

    Python源码 分享57个Python源码,总有一款适合您 57个Python源码下载链接:https://pan.baidu.com/s/1YZcrJAYFFy3OrdEN5IxnQQ?pwd=6 ...

  4. Python源码学习笔记:Python程序执行过程与字节码

    Python程序执行过程与字节码 注:本篇是根据教程学习记录的笔记,部分内容与教程是相同的,因为转载需要填链接,但是没有,所以填的原创,如果侵权会直接删除. 问题: 我们每天都要编写一些Python程 ...

  5. 《Python源码剖析》读书笔记

    <Python源码剖析>电子书下载 http://download.csdn.net/detail/xiarendeniao/5130403 Python源码在官网有下载链接,用ctags ...

  6. python源码加密实现

    本文参考如何保护你的 Python 代码对其想法进行实现. 源码地址:spython@github RAYENCRY rayencry使用AES算法对指定目录中的Python源码加密. 内容 背景 安 ...

  7. 【功能超全】基于OpenCV车牌识别停车场管理系统软件开发【含python源码+PyqtUI界面+功能详解】-车牌识别python 深度学习实战项目

    车牌识别基础功能演示 摘要:车牌识别系统(Vehicle License Plate Recognition,VLPR) 是指能够检测到受监控路面的车辆并自动提取车辆牌照信息(含汉字字符.英文字母.阿 ...

  8. Python源码解读之六 浮点数

    前言 前面的章节都是概括性的描述Python源码中,对象的创建.特性.使用.销毁等,这一章开始我们就要开始分析Python的常见内置类型对象以及对应的实例对象,看看底层具体是如何事项的. 第一个要分析 ...

  9. Python源码剖析[19] —— 执行引擎之一般表达式(2)

    Python源码剖析 --Python执行引擎之一般表达式(2) 本文作者: Robert Chen(search.pythoner@gmail.com ) 3.2     Simple.py 前面我 ...

最新文章

  1. 国民认证科技有限公司助力构建我国可信网络空间
  2. MikroTik Routeros Wlan应用之-pppoe Server
  3. spring装配Bean过程
  4. 作业三--简单四则运算
  5. 1.2.4 TCP/IP参考模型和五层参考模型
  6. 登陆失败bdstoken_解决PanDownload登录提示账号登录失败,获取bdstoken失败问题
  7. 框架设计:实现数据的按需更新与插入的改进--用数据对比进一步说明
  8. 细数开源历史上的十个重大事件
  9. 未检测到其他显示器_如何将 Surface 连接到电视、显示器或投影仪,我教你
  10. 远程Service(AIDL)的简单使用
  11. IntelliJ IDEA优秀插件(编程通用)
  12. gradle问题 cordova
  13. pantone潘通色卡对照表_赶快收藏!快速对色必备:潘通色卡电子版(一)1TPX~113TPX...
  14. Perl脚本的调试方法
  15. (转)什么是DevOps?阿里专家为你来解读
  16. 电子元件知识汇总1-封装
  17. 爬虫--网络数据采集
  18. 基于单片机的智能视力保护监控系统设计
  19. 利用树莓派搭建校园网种子站
  20. 1w存银行一年多少利息_在银行存定期一万块一年有多少利息?

热门文章

  1. pcb 理论阻值、 过孔_超实用!PCB设计中过孔常用的6种处理方式
  2. Linux系统用户环境变量大全,linux系统和用户环境变量的配置文件
  3. linux代码环境配置,linux下配置环境变量【原创】(示例代码)
  4. php88,php88微博
  5. 深蓝学院《从零开始手写VIO》作业七
  6. 【直播课】图像分类竞赛技巧与多标签分类实战
  7. 【知识星球】猫猫狗狗与深度学习那些事儿
  8. 【AI不惑境】网络深度对深度学习模型性能有什么影响?
  9. 全球及中国生产性服务产业动态展望与十四五建设现状规划报告2022版
  10. 山东省能源产业项目动态及未来投资决策建议报告2021版