第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

即使文件相同,filecmp 也會返回 False

即使文件相同,filecmp 也會返回 False

慕桂英546537 2024-01-12 10:33:12
我試圖在兩個(gè)文件之間進(jìn)行比較filecmp,問題是結(jié)果始終是"No, the files are NOT the same",這意味著False 即使文件是相同的。我正在向兩個(gè)不同的文件寫入相同的內(nèi)容。首先我寫入文件revision_1.txt:original_stdout = sys.stdoutwith open('revision_1.txt', 'w') as rev1:       sys.stdout = rev1       print(revision)  # revision is output from command i took beforesys.stdout = original_stdoutif 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.stdoutwith open('revision_2.txt', 'w') as rev2:       sys.stdout = rev2       print(revision)  # revision is output from command i took beforesys.stdout = original_stdout我的目標(biāo)是如果文件相等 - 停止腳本。如果不是,它將重寫revision_2.txt然后發(fā)送郵件,(我已經(jīng)編寫了郵件代碼)。
查看完整描述

3 回答

?
小怪獸愛吃肉

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊

嘗試將shallow設(shè)置為false(默認(rèn)為True),即

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

從文檔中:如果為 true,則具有相同 os.stat() 簽名的文件被視為相等。否則,將比較文件的內(nèi)容。

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


查看完整回答
反對 回復(fù) 2024-01-12
?
皈依舞

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊

如何忽略 message-id (我只需要修訂值)?


查看腳本輸出:不等于修訂版:fpc1-1603878922-228


FFFFFFF 修訂版:fpc1-1603878922-228


FFFFFFFF


腳本:


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)



查看完整回答
反對 回復(fù) 2024-01-12
?
慕雪6442864

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

您對文件的使用不尋常:


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


查看完整回答
反對 回復(fù) 2024-01-12
  • 3 回答
  • 0 關(guān)注
  • 212 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號