-2

The lines in the files will be top to bottom (one under the other)

I have 2 ".txt" file like this :

#john.txt
hello     
my
name
is
John
#jack.txt

second one is like that:

hello
your
name
is
Jack

I will compare this texts line by line but the problem is when I write

"python john.txt jack.txt " on command line sys.arg[1] and sys.argv[2] will provide the lines and compare them . so sys.argv[1] must provide lines in file one by one but I will write"python john.txt jack.txt " only one time. sample output:

python john.txt jack.txt
True
False
True
True
False

I say again. the sys.argv[1] and sys.argv[2] will provide file line by line and when the comparison ends. they will take next lines in files how can I do it ? I think it would done by using while loops but how ?

mrCarnivore
  • 4,638
  • 2
  • 12
  • 29
Aybar Taş
  • 15
  • 1
  • 7
  • The lines in files will be top and bottom !! ( one under the other) – Aybar Taş Dec 06 '17 at 15:44
  • What would the expected output be? – mrCarnivore Dec 06 '17 at 15:47
  • if you call your program in that way (`python john.txt jack.txt`), then `sys.argv` will NOT contain the text files line by line, it will contain the strings `"john.txt"` and `"jack.txt"`. You will then have to `open()` those files in your code and use `getlines()` to load the contents. – Zinki Dec 06 '17 at 15:50

1 Answers1

0

It will help to compare two files line by line and get expected results.

import sys

f1 = sys.argv[1]
f2 = sys.argv[2]

f1_data = open(f1, 'r')
f2_data = open(f2, 'r')

result = [True if f1_line == f2_line else False for f1_line, f2_line in zip(f1_data.readlines(), f2_data.readlines())]

# Result will be in list: [True, False, True, True, False]
print(result)
Anup
  • 200
  • 1
  • 13