程序忽略if​​语句(MD5 Checker)

时间:2017-05-15 20:16:08

标签: python python-3.x if-statement md5

我对编程很新,刚刚开始工作。我被要求出示两个申请。获取并存储文件信息(如文件的名称,大小,上次修改时间和MD5校验和),然后为多个文件写入包含该数据的文件;这个程序运行正常。

我的第二个程序,我将在下面发布代码,应该检查创建的文件并将其与文件的当前条件进行比较。如果MD5总和与其他属性一起变化,则程序不会发出警告。但是,如果所有属性保持不变但MD5发生了变化,那么程序会发出警告,因为出了问题。

我遇到的问题是让检查程序按预期工作。我是一名初学者,所以如果有人愿意看看我的代码,看看我在哪里滑倒,我将不胜感激。

这是我制作的代码。

 files = glob('C:\pytesty\*\*')


filecheck = open('MD5report.dat', 'r')
for fil3 in files:
          fil3md5 = open(fil3, 'rb')
          md5read = fil3md5.read()
          hashy = hashlib.md5(md5read).hexdigest()
          filsize = os.path.getsize(fil3)
          filtime = time.ctime(os.path.getmtime(fil3))
          fline = filecheck.readline()
          [finame, fiMD5, fiSize, fidate, eofline] = fline.split('|')
          print (hashy + fiMD5)

          if (finame == fil3 and fiSize == filsize and fidate == filtime and fiMD5 != hashy):
                    print(finame + ': Wrong md5')
          else:
                    print(finame + ': is OK')

' MD5报告的内容'文件如下:

    C:\pytesty\mynewfolder\egwe.bmp|a41d8cd98f00b204e9800998ecf8427e|0|Mon May  8 19:19:22 2017|
C:\pytesty\mynewfolder\ergeagheghergswa.rtf|8274425de767b30b2fff1124ab54abb5|7|Mon May  8 19:19:33 2017|
C:\pytesty\mynewfolder\tyi,kmykkymktuk.doc|b3e3d42d4a9454f8b6296f3b822f5259|9216|Mon May  8 19:19:13 2017|
C:\pytesty\pyt01\asdf.txt|e1738d5f7e720f3f3f3745b4e0d07e46|33|Mon May  8 19:00:42 2017|
C:\pytesty\pyt01\ex2.txt|7a9d20dd22c96bea3ea1fa0d01085755|69|Mon May  8 19:01:00 2017|
C:\pytesty\pyt01\exampl3.txt|5d29389c5bb758b0d9d5555825daa095|571|Mon May  8 19:01:25 2017|

当我运行程序时,我得到:

d41d8cd98f00b204e9800998ecf8427e   a41d8cd98f00b204e9800998ecf8427e
C:\pytesty\mynewfolder\egwe.bmp: is OK
8274425de767b30b2fff1124ab54abb5   8274425de767b30b2fff1124ab54abb5
C:\pytesty\mynewfolder\ergeagheghergswa.rtf: is OK
b3e3d42d4a9454f8b6296f3b822f5259   b3e3d42d4a9454f8b6296f3b822f5259
C:\pytesty\mynewfolder\tyi,kmykkymktuk.doc: is OK
e1738d5f7e720f3f3f3745b4e0d07e46   e1738d5f7e720f3f3f3745b4e0d07e46
C:\pytesty\pyt01\asdf.txt: is OK
7a9d20dd22c96bea3ea1fa0d01085755   7a9d20dd22c96bea3ea1fa0d01085755
C:\pytesty\pyt01\ex2.txt: is OK
5d29389c5bb758b0d9d5555825daa095   5d29389c5bb758b0d9d5555825daa095
C:\pytesty\pyt01\exampl3.txt: is OK
>>> 

正如您所看到的,该程序认为所有文件都是“好的”。尽管事实并非如此,因为我故意改变了报告文件中的第一个MD5校验和。

我试图寻找一些解决方案,但它们要么与这种特殊情况无关,要么根本无法解决。

非常感谢一些帮助。

根据评论编辑:

d41d8cd98f00b204e9800998ecf8427e   a41d8cd98f00b204e9800998ecf8427e
C:\pytesty\mynewfolder\egwe.bmp   C:\pytesty\mynewfolder\egwe.bmp
0   0
Mon May  8 19:19:22 2017   Mon May  8 19:19:22 2017
C:\pytesty\mynewfolder\egwe.bmp: is OK
8274425de767b30b2fff1124ab54abb5   8274425de767b30b2fff1124ab54abb5
C:\pytesty\mynewfolder\ergeagheghergswa.rtf   C:\pytesty\mynewfolder\ergeagheghergswa.rtf
7   7
Mon May  8 19:19:33 2017   Mon May  8 19:19:33 2017
C:\pytesty\mynewfolder\ergeagheghergswa.rtf: is OK
b3e3d42d4a9454f8b6296f3b822f5259   b3e3d42d4a9454f8b6296f3b822f5259
C:\pytesty\mynewfolder\tyi,kmykkymktuk.doc   C:\pytesty\mynewfolder\tyi,kmykkymktuk.doc
9216   9216
Mon May  8 19:19:13 2017   Mon May  8 19:19:13 2017
C:\pytesty\mynewfolder\tyi,kmykkymktuk.doc: is OK
e1738d5f7e720f3f3f3745b4e0d07e46   e1738d5f7e720f3f3f3745b4e0d07e46
C:\pytesty\pyt01\asdf.txt   C:\pytesty\pyt01\asdf.txt
33   33
Mon May  8 19:00:42 2017   Mon May  8 19:00:42 2017
C:\pytesty\pyt01\asdf.txt: is OK
7a9d20dd22c96bea3ea1fa0d01085755   7a9d20dd22c96bea3ea1fa0d01085755
C:\pytesty\pyt01\ex2.txt   C:\pytesty\pyt01\ex2.txt
69   69
Mon May  8 19:01:00 2017   Mon May  8 19:01:00 2017
C:\pytesty\pyt01\ex2.txt: is OK
5d29389c5bb758b0d9d5555825daa095   5d29389c5bb758b0d9d5555825daa095
C:\pytesty\pyt01\exampl3.txt   C:\pytesty\pyt01\exampl3.txt
571   571
Mon May  8 19:01:25 2017   Mon May  8 19:01:25 2017
C:\pytesty\pyt01\exampl3.txt: is OK

1 个答案:

答案 0 :(得分:1)

如果符合所有其他条件,则您的代码仅打印哈希值不匹配。因此,如果由于某种原因,您的文件名,日期或大小不相同,如果永远不会执行表示哈希错误的代码。它将直接转到说文件正常的行。

我建议分别检查每个条件,以确保显示正确的错误消息。