利用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的Tkinter库做了一个界面,感觉这个库使用起来还是挺方便的,音乐的数据来自网易云音乐的一个接口,通过urllib.urlopen模块打开网址,使用Js...

Flask 上传自定义头像的实例详解

Flask 上传自定义头像的实例详解

Flask Web 开发这本书基本上做完了,后面还需要温习,但是自己做的博客总觉得简陋了点,所以,在动脑子开发新功能 今天想到最基本的功能,自定义头像 那这样的功能,设计到2大基本功能块...

Django 生成登陆验证码代码分享

Django 生成登陆验证码代码分享

环境准备 python3.52 pycharm5.05 Pillow 自制的验证码工具包/utils/check_code 验证码的作用 防恶意破解密码:防止,使用程序或...

Python多继承以及MRO顺序的使用

多继承以及MRO顺序 1. 单独调用父类的方法 # coding=utf-8 print("******多继承使用类名.__init__ 发生的状态******") class...

Python中浅拷贝copy与深拷贝deepcopy的简单理解

以下是个人对Python深浅拷贝的通俗解释,易于绕开复杂的Python数据结构存储来进行理解! 高级语言中变量是对内存及其地址的抽象,Python的一切变量都是对象。 变量的存...