【问题标题】:filecmp returns False even though files are equal即使文件相等,filecmp 也会返回 False
【发布时间】:2020-10-26 12:26:39
【问题描述】:

我试图用filecmp 比较两个文件,问题是结果总是"No, the files are NOT the same" 这意味着False 即使文件相同。

我正在向两个不同的文件写入相同的内容。首先我写文件revision_1.txt:

original_stdout = sys.stdout
with open('revision_1.txt', 'w') as rev1:
       sys.stdout = rev1
       print(revision)  # revision is output from command i took before
sys.stdout = original_stdout

if filecmp.cmp('revision_1.txt', 'revision_2.txt'):
    # revision_2.txt is file I c
    print("Both the files are same")
else:
    # Do whatever you want if the files are NOT the same
    print("No, the files are NOT the same")

original_stdout = sys.stdout
with open('revision_2.txt', 'w') as rev2:
       sys.stdout = rev2
       print(revision)  # revision is output from command i took before
sys.stdout = original_stdout

我的目标是如果文件相等 - 停止脚本。如果不是,它将重写revision_2.txt,然后发送邮件,(我已经写了邮件的代码)。

【问题讨论】:

  • 你为什么这么操纵sys.stdout?为什么不直接做file.write(...)

标签: python file-comparison


【解决方案1】:

您对文件的使用不寻常:

import filecmp

revision = "08/15"

with open('revision_1.txt', 'w') as rev1:
      rev1.write(revision)

with open('revision_2.txt', 'w') as rev2:
      rev2.write(revision)

with open('revision_3.txt', 'w') as rev3:
      rev3.write(revision + "-42")

# should compare equal
if filecmp.cmp('revision_1.txt', 'revision_2.txt'):
    print("Identical")
else:
    print("No, the files are NOT the same")

# should NOT compare equal
if filecmp.cmp('revision_1.txt', 'revision_3.txt'):
    print("Identical")
else:
    print("No, the files are NOT the same")

打印

Identical
No, the files are NOT the same

【讨论】:

  • 怎么能忽略message-id(我只需要Revision值)?
  • ` 不等于 修订:fpc1-1603878922-228 FFFFFFF Revision: fpc1-1603878922-228 FFFFFFF `
【解决方案2】:

尝试将 shallow 设置为 false(默认为 True),即

if filecmp.cmp('revision_1.txt', 'revision_2.txt', shallow=False):

来自文档: 如果 shallow 为真,则具有相同 os.stat() 签名的文件被视为相等。否则,将比较文件的内容。

https://docs.python.org/3/library/filecmp.html#filecmp.cmp

【讨论】:

  • 添加一个关于该论点应该做什么的小解释可能会有所帮助
【解决方案3】:

谢谢大家的回复 正如我所说,我对 Python 很陌生 根据你的建议我改了代码,这次我要发完整的脚本解释一下

我成功比较了 'revision' 和 'd' 我的问题是我得到了不同的 rpc-reply message-id,

怎么能忽略message-id(我只需要Revision值)?

查看脚本输出: 不相等 修订:fpc1-1603878922-228

FFFFFFF 修订:fpc1-1603878922-228

FFFFFFF

脚本:

import smtplib
import email.message
from email.mime.text import MIMEText
from ncclient import manager
from ncclient.xml_ import *
import sys
import time
import filecmp

# Connecting to juniper cc-vc-leg
conn = manager.connect(
        host='10.1.1.1',
        port='830',
        username='test',
        password='test',
        timeout=10,
        device_params={'name':'junos'},
        hostkey_verify=False)

# Take juniper commands
resault = conn.command('show version | match Hostname', format='text')
revision = conn.command('show system commit revision', format='text')
compare_config = conn.compare_configuration(rollback=1)

# Open & read file vc-lg_rev.text
f = open('vc-lg_rev.text', 'r')
d = f.read()

# Check if revision output is equal to file "vc-lg_rev.text"
# If equal exit the script
if  (revision == d):
       print('equal')
       exit()
       print('I hop script stopped')
else:
       print('Not equal')
       print(revision)
       print('FFFFFFF')
       print(d)
       print('FFFFFFF')

# To save last revision number to "vc-lg_rev.text"
with open('vc-lg_rev.text', 'w', buffering=1) as rev1:
        rev1.write(str(revision))
        rev1.flush()
rev1.close()


# This is how i copy "compare_config" output to file "vc-lg_compare.text"
original_stdout = sys.stdout
with open('vc-lg_compare.text', 'w') as a:
        sys.stdout = a
        print(compare_config)
sys.stdout = original_stdout

def send_email(compare):
    server = smtplib.SMTP('techunix.technion.ac.il', 25)
    email_reciver = 'rafish@technion.ac.il', 'rafi1shemesh@gmail.com'
    message = f"'Subject': mail_subject \n\n {compare}"
    ID = 'Juniper_Compare'
    server.sendmail(ID, email_reciver, message)

with open('vc-lg_compare.text', 'r') as compare:   # "as" means file object called compare
        text = str(compare.read())                 # I want to recive the output as string to look specific word in the file
        if (text.find('+') > -1) or (text.find('- ') > -1):
                send_email(text)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多