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
  Find duplicate files in multiple directories Pavel_47 9 2,878 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  count every 28 files and find desire files RolanRoll 3 1,999 Dec-12-2021, 04:42 PM
Last Post: Axel_Erfurt
  Have to use Python 2.x to find "yesterday' files tester_V 6 2,748 Sep-19-2021, 12:26 AM
Last Post: tester_V
  find 'yesterdays files' tester_V 8 3,709 Jun-18-2021, 02:10 AM
Last Post: tester_V
  Find and replace in files with regex and Python Melcu54 0 1,815 Jun-03-2021, 09:33 AM
Last Post: Melcu54
  how to find difference between fake driving license or real using python? pjaymn 2 2,482 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 3,553 Dec-11-2020, 07:07 PM
Last Post: tester_V
  helping PyInstaller To Find files Harshil 0 1,434 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 3,470 Aug-25-2020, 01:52 AM
Last Post: tester_V
  Find all “*.wav” files that created yesterday on linux host with python. pydeev 6 4,685 Jan-07-2020, 06:43 AM
Last Post: pydeev

Forum Jump:

User Panel Messages

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