pythonlinuxsortingdifferencepasswd

How to skip new lines for comparison in python?


I have a python code which compare 2 text file and show if they have differences. first of all i sort files then remove their spaces that are at the end of text after sort then do the comparison.

#!/usr/bin/env python

import difflib
from pathlib import Path

path1=Path('/data1/Dir/thesaurus/file1')
path2=Path('/data1/Dir/thesaurus/file2')

text1 = open(str(path1)).readlines()
text1.sort(key=lambda x: x.strip('#').rsplit('.', 1)[0])
text2 = open(str(path2)).readlines()
text2.sort(key=lambda x: x.strip('#').rsplit('.', 1)[0])

for line in difflib.unified_diff(text1, text2, n=0):
print line,

actually those files for comparison are the /etc/passwd of different servers. this code worked good but recently i found out that it is not returning the correct output for some servers because of \n. how i can eliminate these \n in my comparison?

for example, the file1 and file2 have this content:

@=> cat file1
wise:x:wuser:/home/wise:/sbin/nologin
nafs:x:user:/home/nafs:/sbin/nologin
khor:x:khor:/home/khor:/bin/bash
jari:x:user:/home/jari:/sbin/nologin
test:x:Test:/home/test:/sbin/nologin
zabbix:x:Zabbix Agent:/home/zabbix:/usr/sbin/nologin
@=> cat file2
wise:x:wuser:/home/wise:/sbin/nologin
nafs:x:user:/home/nafs:/sbin/nologin
jari:x:user:/home/jari:/sbin/nologin
zabbix:x:Zabbix Agent:/home/zabbix:/usr/sbin/nologin

what i expect:

---
+++
@@ -6 +5,0 @@
-khor:x:khor:/home/khor:/bin/bash
@@ -8 +7 @@
-test:x:Test:/home/test:/sbin/nologin  

what i get:

---
+++
@@ -1 +1 @@
-nafs:x:user:/home/nafs:/sbin/nologin
+nafs:x:user:/home/nafs:/sbin/nologin
@@ -6 +5,0 @@
-khor:x:khor:/home/khor:/bin/bash
@@ -8 +7 @@
-jari:x:user:/home/jari:/sbin/nologin
+jari:x:user:/home/jari:/sbin/nologin
@@ -12 +10,0 @@
-test:x:Test:/home/test:/sbin/nologin

i ran my code without the comparison part and printed the text1 and text2 to find the problem and i saw that there is an \n next to lines that they were returned incorrectly in one file and not in other file.


Solution

  • Strip each line?

    text1 = [l.strip() for l in open(str(path1))]