Python Forum
python 3 find difference between 2 files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python 3 find difference between 2 files
#1
Hi,
I'm working a file system monitoring script. When I compare two files in the below script, it's returning non-zero value even though both files are same. I checked for any special characters also.
When I run the diff command on shell it shows no difference and echo $? returns zero. Please help me solve this issue.

[root@]# cat fs.txt
home=17
apps=7
work=54

[root@log]# cat apps_WARN_20200514-1210
7
[root@log]# cat apps_WARN_20200514-1211
7

[root@]# ./check_fs_usage.py
0a1
> 7
1


#!/usr/bin/env /opt/anaconda3/bin/python

import datetime
import os
import sys
import subprocess

DT = datetime.datetime.now().strftime("%Y%m%d-%H%M")
FP = '/root/pyscripts/log/'

with open ('/root/pyscripts/log/fs.txt', 'r') as FS:
    for x in FS.readlines():
        if int(x.split('=')[1]) >= 5 and int(x.split('=')[1]) <=10:
            with open(FP + x.split('=')[0] + "_WARN_" + DT, "w") as WF:
                WF.write(x.split('=')[1])
                subprocess.call("ls -1tr | grep " + x.split('=')[0] + "_WARN| head -n -2 | xargs -d '\n' rm -f --", shell=True)
                FN = WF.name
                FN = FN.split('/')[4]
                OldFN = subprocess.check_output("ls -1tr | grep " + x.split('=')[0] + "_WARN | grep -v " + FN, shell=True)
                OldFN = str(OldFN, 'utf-8').strip()
                os.chdir(FP)
                STATUS = subprocess.call('diff ' + FN + ' ' + OldFN, shell=True)
                print(STATUS)
                
I also tried, but same error:
STATUS = subprocess.call('diff ' + x.split("=")[0] + '_WARN*  > /dev/null 2>&1', shell=True)
Reply
#2
I tried with filecop module as well. It seems like the issue is with files that are created through the script.
If I create file manually and run it through python script it works.

OldFN = (FP + OldFN)
FN = (FP + FN)
print(FN)
print(OldFN)
STATUS =  filecmp.cmp(OldFN, FN, shallow=False)
Reply
#3
you may be experiencing a difference in line terminator.
Before comparing lines, use strip, after line 12, add: x = x.strip()
try that

Actually that's not going to work here, but keep it in mind.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to find difference between fake driving license or real using python? pjaymn 2 199 Dec-20-2020, 08:41 PM
Last Post: jefsummers
  List of error codes to find (and count) in all files in a directory tester_V 8 295 Dec-11-2020, 07:07 PM
Last Post: tester_V
  helping PyInstaller To Find files Harshil 0 298 Aug-30-2020, 10:16 AM
Last Post: Harshil
  Find specific subdir, open files and find specific lines that are missing from a file tester_V 8 761 Aug-25-2020, 01:52 AM
Last Post: tester_V
  Project files, Python files Heyjoe 4 538 Jun-27-2020, 07:30 PM
Last Post: Heyjoe
  Find all ā€œ*.wavā€ files that created yesterday on linux host with python. pydeev 6 1,215 Jan-07-2020, 06:43 AM
Last Post: pydeev
  Difference Between 2 files enigma619 3 629 Dec-21-2019, 01:39 PM
Last Post: Gribouillis
  How to find sum difference between two csv Prince_Bhatia 3 777 Jul-30-2019, 05:35 AM
Last Post: ndc85430
  Python Script to Produce Difference Between Files and Resolve DNS Query for the Outpu sultan 2 723 May-22-2019, 07:20 AM
Last Post: buran
  Socket creation speed difference Python 2.7 / Python 3.7 PiAil 1 955 Feb-13-2019, 01:55 PM
Last Post: PiAil

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020