How to make the program write all information?

Asked

Viewed 220 times

0

print('Bem-vindo!')
print()

numero_contas = int(input('Deseja registrar quantas contas: '))
x = 0

while x < numero_contas:
            x = x + 1
            conta = str(input('A conta é de qual site:'))                                  
            print()
            usuário = str(input('Digite o usuário:'))
            senha = str(input('Digite a senha: '))
            print()
            arq = open('Contas.txt', 'w')
            arq.write(conta)
            arq.write('\n')
            arq.write('Usuário: {}'.format(usuário))
            arq.write('\n')
            arq.write('Senha: {}'.format(senha))
            arq.write('\n')
            arq.close()

The problem is this: if, for example, I type 2 in the "account" variable, it asks for the information normally, but when I open the file to check, it only records the first account, if I type 3 in the "account" variable, it only records the second account, so on. How can I solve this problem?

One more basic question, how can I make the program, whenever it runs, not take the records made previously? For example: I ran the program, registered only one account. Then, I run the program again, and record two accounts, then when I go to check, the data from the first execution is gone, leaving only the data from the second execution recorded. And I want him to record all the data, without deleting the data from the previous executions, I want him to join with the next executions.

1 answer

0


It’s because you’re wearing w, use a (an "extra" is that you really don’t need to open multiple times the same file may leave Handle’s out of the while)

print('Bem-vindo!')
print()

numero_contas = int(input('Deseja registrar quantas contas: '))
x = 0

arq = open('Contas.txt', 'a')

while x < numero_contas:
    x = x + 1
    conta = str(input('A conta é de qual site:'))
    print()
    usuário = str(input('Digite o usuário:'))
    senha = str(input('Digite a senha: '))
    print()
    arq.write(conta)
    arq.write('\n')
    arq.write('Usuário: {}'.format(usuário))
    arq.write('\n')
    arq.write('Senha: {}'.format(senha))
    arq.write('\n')

arq.close()

Or you can put the open if you need that every time the program starts it generate the list again use so:

print('Bem-vindo!')
print()

numero_contas = int(input('Deseja registrar quantas contas: '))
x = 0

arq = open('Contas.txt', 'w')

while x < numero_contas:
    x = x + 1
    conta = str(input('A conta é de qual site:'))
    print()
    usuário = str(input('Digite o usuário:'))
    senha = str(input('Digite a senha: '))
    print()

    arq.write(conta)
    arq.write('\n')
    arq.write('Usuário: {}'.format(usuário))
    arq.write('\n')
    arq.write('Senha: {}'.format(senha))
    arq.write('\n')

arq.close()
  • Oops, thanks, solved the problem!!!

  • So the "a" function is not to have to open the same file several times?

  • Just one more question, how can I open the file in certain place I wish?

  • The w "erases" (sets the pointer at the beginning) when using the open (not in write) and generates the contents of the file again, the a only add at the end of the file if it exists. @Brkappa

  • It’s type for example, I want it to open the file that is in some directory, for example: C: User Accounts.txt Directory

  • In the python open, for example: I have the file Accounts.TXT in the directory: C: Documents Pasta_test Accounts.txt. I want to open the file "Accounts.TXT" that is in this directory.

  • @Brkappa I think it would be this in windows open('c:/Documentos/Pasta_Teste/Contas.txt', 'a') and this on Linux open('/home/user/Documentos/Pasta_Teste/Contas.txt', 'a')

  • 1

    Thanks, solved all my problems!!!!

  • @Brkappa ;D for nothing

Show 4 more comments

Browser other questions tagged

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