利用python计算windows全盘文件md5值的脚本

yipeiwu_com5年前Python基础
import hashlib
import os
import time
import configparser
import uuid
 
def test_file_md5(file_path):
  test = hashlib.md5()
 
  if os.path.isfile(file_path):
    with open(file_path, "rb") as f:
      while True:
        data = f.read(8096)
        if not data:
          break
        else:
          test.update(data)
      ret = test.hexdigest()
      config = configparser.ConfigParser()
 
      config.read("E:/python/pycharm/再开次开始/前端/test_md5.ini",encoding="utf-8")
      if config.has_section(os.path.basename(file_path)):
        new_section_name = str(os.path.basename(file_path)) + ":" + str(uuid.uuid4())
        config[new_section_name] = {"文件路径":os.path.dirname(file_path),
                 "md5值":ret}
      else:
        config[os.path.basename(file_path)] = {"文件路径": os.path.dirname(file_path),
                        "md5值": ret}
      config.write(open("E:/python/pycharm/再开次开始/前端/test_md5.ini","w",encoding="utf-8"))
 
 
 
def test_dir_md5(file_path):
  test_abs_path = os.path.abspath(file_path)
  # print(test_abs_path)
  os.chdir(test_abs_path)
  for file in os.listdir(os.getcwd()):
    if os.path.isfile(file):
      test_file_md5(os.path.abspath(file))
    elif os.path.isdir(file):
      test_dir_md5(os.path.abspath(file))
    else:
      pass
  # return True
 
 
if __name__ == '__main__':
  began_path = os.getcwd()
  test_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(began_path))))
  os.chdir(test_path)
  print(os.listdir())
  for test_file in os.listdir():
    os.chdir(test_path)
    if os.path.abspath(test_file).startswith("E:\\$"):
      continue
    else:
      if os.path.isfile(test_file):
        # print("yyyyy")
        test_file_md5(os.path.abspath(test_file))
      elif os.path.isdir(test_file):
        # print("hahah")
        test_dir_md5(os.path.abspath(test_file))
        # print(os.path.abspath(test_file))
      else:
        pass

结果如下

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python使用正则表达式来获取文件名的前缀方法

在我们处理文件的时候,会遇到这样的一种场景,我们需要对某个文件进行操作,然后生成与原文件名相同的文件(只是文件格式改变)。那么这个时候就可以使用正则表达式来匹配我们所需要的字符串。 实现...

Python读取word文本操作详解

Python读取word文本操作详解

本文研究的主要问题时Python读取word文本操作,分享了相关概念和实现代码,具体如下。 一,docx模块 Python可以利用python-docx模块处理word文档,处理方式是面...

利用Django模版生成树状结构实例代码

利用Django模版生成树状结构实例代码

前言 我们经常会有这样的需求,比如评论功能,每个评论都有可能会有自己的子评论,如果在界面只展示成一列的话非常不美观,也不能体现出他们的层级关系。那么我们今天就来看看如何使用Django的...

Python列表原理与用法详解【创建、元素增加、删除、访问、计数、切片、遍历等】

Python列表原理与用法详解【创建、元素增加、删除、访问、计数、切片、遍历等】

本文实例讲述了Python列表原理与用法。分享给大家供大家参考,具体如下: 列表的基本认识 列表简介 列表的创建 基本语法[]创建 list()创建...

HTML的form表单和django的form表单

HTML的form表单和django的form表单

django的表单系统,分2种 基于django.forms.Form的所有表单类的父类 基于django.forms.ModelForm,可以和模型类绑定的Form 直接用...