• Python校验文件MD5值


    import hashlib
    import os
    
    
    def GetFileMd5(filename):
        if not os.path.isfile(filename):
            return
        myHash = hashlib.md5()
        f = open(filename,'rb')
        while True:
            b = f.read(8096)
            if not b :
                break
            myHash.update(b)
        f.close()
        return myHash.hexdigest()
    
    print(GetFileMd5('/Users/binyun007/Desktop/xxx')) #文件路径

    窗口模式, 需要pip安装PyQt5

    import sys
    import os
    import hashlib
    from PyQt5.QtWidgets import QApplication, QWidget, QPushButton,  QMessageBox
    from PyQt5.Qt import QLineEdit
    
    class App(QWidget):
    
        def __init__(self):
            super().__init__()
            self.title = 'MD5校验'
            self.left = 800
            self.top = 600
            self.width = 320
            self.height = 200
            self.initUI()
    
        def initUI(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)
    
            # create textbox
            self.textbox = QLineEdit(self)
            # self.textbox.setText('/Users/binyun007/Desktop/') #设置文本框的默认值
            self.textbox.setText(FileRecord.readpath()) #读取文本框的默认值
            self.textbox.move(20, 20)
            self.textbox.resize(280, 40)
    
            # Create a button in the window
            self.button = QPushButton('校验', self)
            self.button.move(20, 80)
    
            # connect button to function on_click
            self.button.clicked.connect(self.on_click)
            self.show()
    
        def on_click(self):
            textboxValue = self.textbox.text()
            md5 = GetFileMd5(textboxValue)
            QMessageBox.question(self, "Message", 'MD5:' + md5,
                                 QMessageBox.Ok,QMessageBox.Ok)
            """打印完毕之后设置文本框默认值为上一次使用后的"""
            FileRecord.writpath(textboxValue)
            #self.textbox.setText(textboxValue) #
    
    #保存、读取MD5记录
    class FileRecord():
    
        #保存
        def writpath(filepath):
            with open('md5.txt','w') as f:
                f.write(filepath)
    
        #读取
        def readpath():
            try:
                with open('md5.txt','r') as f:
                    record = f.readline()
                    return record
                
            #如果文件不存在创建
            except FileNotFoundError:
                with open('md5.txt','w') as f:
                    return
    
    
    #校验MD5值
    def GetFileMd5(filename):
        if not os.path.isfile(filename):
            return
        myHash = hashlib.md5()
        f = open(filename,'rb')
        while True:
            b = f.read(8096)
            if not b :
                break
            myHash.update(b)
        f.close()
        return myHash.hexdigest()
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = App()
        app.exit(app.exec_())
  • 相关阅读:
    sharpen和filter2D
    Changing the contrast and brightness of an image!(opencv对比度和亮度调节)
    人脸表情识别
    Pycharm下载和安装
    Anaconda下载与安装
    图像人脸检测+人眼检测 (opencv + c++)
    cv2.VideoWriter()指定写入视频帧编码格式
    python_openCV例程遇到error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale的简单解决方法
    图像处理库 Pillow与PIL
    IDE bulid构建隐藏了什么(预处理->编译->汇编->链接)
  • 原文地址:https://www.cnblogs.com/roc-fly/p/10103138.html
Copyright © 2020-2023  润新知