PYTHON - Storage of values, line by line, in txt

Asked

Viewed 252 times

0

How do I store different values, row by row, in a txt file without deleting the previous one?

  • Include your code to the question

1 answer

1

You use the a of append in the open and added to the \n The n jumps a line. So it would be something like

teste = 'linha um' + '\n'
arq = open('arquivo.txt','a')
arq.write(teste)
arq.write('123'+'\n')

Normally a list is used, and if you run a is, changing the parameter of the .write A more extensive example:

def cadastrar():
    nome   = raw_input('INFORME O NOME DO CLIENTE: ')
    cpf    = raw_input('INFORME O CPF DO CLIENTE: ')
    senha  = raw_input('INFORME A UMA NOVA SENHA: ')
    saldo  = raw_input('INFORME CASO EXISTA SALDO INICIAL: ')

    arq = open('clientes/' + cpf + '.txt', 'w')
    arq.write(nome+'\n')
    arq.write(cpf+'\n')
    arq.write(senha+'\n')
    if saldo != '':
        arq.write(saldo+'\n')
    arq.close()

Version of python: 2.7

Browser other questions tagged

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