0
votes

I'm trying to compare between two files with filecmp, the problem is that the result is always "No, the files are NOT the same" which means False even though the files are the same.

I'm writing to two different files the same content. First I write to file 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

My goal is if the files are equal - stop the script. If they are not, it will rewrite revision_2.txt and then send mail, (I already wrote the code for mail).

3
Why do you manipulate sys.stdout so much? Why not just do file.write(...)?Tomerikoo

3 Answers

0
votes

Your usage of files us unusual:

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")

prints

Identical
No, the files are NOT the same
0
votes

Try set shallow to false (Default is True), i.e

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

From the documentation: If shallow is true, files with identical os.stat() signatures are taken to be equal. Otherwise, the contents of the files are compared.

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

0
votes

Thank you all for the reply As I said I'm very new with Python According to your recommendations I changed the code, this time I'm going to send the full script and explain

I successes to compare between 'revision' and 'd' my problem is that I'm getting different rpc-reply message-id,

How can can ignore message-id (I only need the Revision value) ?

See script output: Not equal Revision: fpc1-1603878922-228

FFFFFFF Revision: fpc1-1603878922-228

FFFFFFF

Script:

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)