Python file comparison program

Asked

Viewed 2,777 times

5

Personal I am with a doubt and a great difficulty to create a program that reads 2 files, example doc1.txt and doc2.txt, within the files has the following data:

doc1.txt   doc2.txt
1            1
1            2
2            5
3            6
4            7
5            7
10           8
             9

It should be line by line and compare, for example if doc1 data 1 is == to doc2 data 1, doc1 > doc2 or <, record the data in a doc called result.txt in sequence with each given line by line. He must record the data in sequence.

Just follow my code:

#Arquivos de Texto
with open('doc1.txt', 'r', encoding='UTF-8') as doc1:
    arq1: int= doc1.readline()

with open('doc2.txt', 'r', encoding='UTF-8') as doc2:
    arq2: int = doc2.readline()

if arq1 == arq2:
    with open('resultado.txt', 'w') as res:
         r: int = res.write(arq1)

elif arq1 > arq2:
    with open('resultado.txt', 'w') as res:
         r: int = res.write(arq1)

elif arq1 < arq2:
    with open('resultado.txt', 'w') as res:
         r: int = res.write(arq2)
else:
    print('Error')

It only compares the first data 1 == 1 and writes to the file resultado.txt and then he doesn’t read any more data from the files.

I don’t know what I’m doing wrong.

  • You used the method readline which, as the name says, reads a line. If you want to compare all the lines, you need to iterate on them. It is not clear what needs to be saved in the result file. It will always be the highest among the values or the file name?

  • Then I must use readlines, to read all lines, the data that will be written in the result file will be compared, example both doc1 and doc2 files have 1, then the files will be compared, if 1 is equal to 1, will be written in the result file, ai it will compare the next line 1 is equal to 2, no, 1 is greater than 2, no, 1 is less than 2 yes then write to the result, it always reads the first file and is saved in the variable to then compare with the next one, until all the result is in sequence ex: 1,2,3,4,5,6,7,8,9,10. is more or less that.

  • @Guischolzportela: What if the files don’t have the same number of lines ? What is the expected output for the example you gave ?

  • Worse than I don’t know, I used to do a program in Cobol called balance line, in other words, he was comparing numbers online per line, as I explained above, then he was recording in sequence in numerical order per line, regardless of the amount of data inside the files, some files had 1000 lines and another 500 lines, and still worked.

1 answer

4


As commented, the problem of reading only the first line is that you used the method readline, that reads only one line. To read them all, you will need to iterate over the file with a repeat loop.

As explained in What’s with no Python for?, you can use multiple contexts managers in the same with, which would simplify your program:

with open('entrada_1.txt') as arquivo_1, \
     open('entrada_2.txt') as arquivo_2, \
     open('saida.txt', 'w') as saida:
    numeros_arquivo_1 = (int(numero.strip()) for numero in arquivo_1)
    numeros_arquivo_2 = (int(numero.strip()) for numero in arquivo_2)
    for a, b in zip(numeros_arquivo_1, numeros_arquivo_2):
        saida.write(f'{a}\n' if a >= b else f'{b}\n')

Since you are reading a file, it is natural that the data come as string, then it is necessary to convert them to numerics. Objects numeros_arquivo_1 and numeros_arquivo_2 are exactly that - the strip is to remove line break.

With the zip, we group the values of the two files to facilitate the comparison between a and b. If the value in file 1 is greater than or equal to that of file 2, the value of a, otherwise, the value of b. It is important to point out that the zip uses the smallest file as a reference, ignoring the extra values of the largest. If you need to compare based on the largest file, use the function itertools.zip_longest.

  • I get it, I’m gonna follow your advice, I’m gonna test it, and as soon as any questions come up, I’m gonna put it here, and also after I finish the program, I’ll put it here, you can help someone in the same trouble I do. Thank you very much, I’ll keep you informed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.