Duplicate files

Asked

Viewed 45 times

-3

Good Afternoon,

I’m very new to Python (2 months) and this is my first post here! I’m creating a script that helps me read two files:

-- 1 file with 51 lines (each line has a different TSAT113806)

-- File 2 with 58 lines ( each line has a TSAT113806 equal to file 1 and 7 different)

What I want to do is create a script that tells me:

  • what numbers are repeated in file 1 and 2
  • save those numbers to a file 3.

I made this code, but it’s not working, you can help me?

import os
def NovoDoc():

    with open('Pedentes.csv', 'r') as pendente:
        arq1: int = pendente.readlines()

    with open('Cancelados.csv' , 'r') as cancelamentos:
        arq2: int = cancelamentos.readlines()

    if arq1 == arq2:
        with open(os.path.join('resultado.csv', 'w')) as res:
            r: int = res.write(arq1)
    else:
        print('Erro')
  • vc is halfway through, the most attention-grabbing error is 'arq1: int =', it should be 'arq1=', without the colon and int

  • Hello @Eltonnunes, thanks for the tip.. What happens is that I run and then nothing happens! I’m a little lost there!

1 answer

0


with open('Pedentes.csv') as pendente:                                          
    arq1 = pendente.readlines()

with open('Cancelados.csv') as cancelamentos:
    arq2 = cancelamentos.readlines()

with open('resultado.csv', 'w') as res:
    for i in arq1:
        for y in arq2:
            if arq1 in arq2:
                    res.write(arq1 + '\n')
                    print('valor escrito')
            else:
                print('Erro')

by default open uses 'r', so when reading a file you won’t need to put 'r''

open(Arq, 'r') is the same as open(Arq)

maybe you are confusing the header of a function with the definition of a variable

variable = value

turn the time you call open(Arq, 'w'), it goes on write the file and you have lost all yours however, so use only once, only close the file after you finish writing everything what you need

.readlines() returns a list that you can use a loop to go through it

arq1 == arq2 is comparing the file as a whole, not individual lines

  • Hello @Elton Nunes, thanks for your comment, your code actually works, but when I print (in the notebook juyter) it gives error!. I noticed that it actually creates the Output file but does not print anything in the file. Am I doing something wrong? Thanks for your help one more time.

  • what are you printing? and how are you doing it?

  • Then I did this res = open ('result.csv', 'r') result = res.read() res.close() print(result) .

  • I made a change to the script, it will make a print every time you write to the file, run it to see if you are writing

Browser other questions tagged

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