PYTHON - Value store, line by line, in txt

0

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

    
asked by anonymous 16.03.2018 / 17:59

1 answer

1

You use a of append in open and sum with \n \ n makes you skip a line. So it would be something like

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

Usually a list is used, and if you run a for, changing the parameter of .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()

Python version: 2.7

    
16.03.2018 / 18:13