How to print and save to a TXT

Asked

Viewed 250 times

0

Good morning, I’d like to take the prints print inside a txt file, already saving automatically.

print ("Informe o CNPJ: ", cnpj) 
print ("Resumo do Chamado: ", resumo)
print ("Nome do correspondente: ", nome)
print ("E-mail: ", email)
print ("Descrição da solicitação/Incidente: ", problema)
print ("Troubleshooting: ", trouble)
print ("Solução: ", solucao)
print ("Caminho do HelpFile: ", caminho)
  • You want to redirect your print directly to a file or want to print the same thing on the screen and in the file independently?

  • 1

    Could you explain your idea better? It’s not very clear to me what you want

2 answers

1


The ideal would be to save what would be these prints to a variable and then save to a file. For example:

textoParaSalvar = '''Informe o CNPJ: %s
Resumo do Chamado: %s
Nome do correspondente: %s
E-mail: %s
Descrição da solicitação/Incidente: %s
Troubleshooting: %s
Solução: %s
Caminho do HelpFile: %s 
''' % (cpf, resumo, nome, email, problema, trouble, solucao, caminho)

with open("ARQUIVO.TXT", 'w')  as arquivo:
    arquivo.write(textoParaSalvar)

Explaining the code

I create the variable textoParaSalvar and assign a string. Note that I used %s to indicate the position where I should insert another string into it. This is string formatting, see here an interesting site about it.

To open the file, I used withopen(arquivo, modo) as nome:.
For the function open() i called with two arguments. The first should indicate the name of the file I want to save, the second the way I am opening it.

There are basically four modes for opening files:

  • r: Used to open in read mode (default, can be omitted if you want to use it)
  • w: Used to open in writing mode
  • to: Used for when you want to open in mode append, which would be a read mode that does not delete existing content.
  • x: Used only for file creation.

Modes can be expanded, for more see official documentation.

  • 1

    Missed the s of %s in the second line

  • I just corrected

  • He doesn’t need to give n to jump?

  • When I use triple quotes it is not necessary. The line break itself in the code already indicates line break in the output.

  • Thank you very much friend.

  • Friend, because the use of 'w' no - with open......

  • I supplemented the answer.

Show 2 more comments

0

You can run from the terminal and save the outputs. Like, if you’re on Ubuntu:

python3 meuprograma.py -> touch outputs.txt

This saves all your prints executed in the program in a text file (called outputs, in this case).

  • But what to do in Windows even? has how? It save the prints without deleting the others?

  • Yes, but you have to configure python to work on cmd. Then you use python myscript.py >> output.txt That way you will overwrite everything, but you can add it again. This feature of directing outputs from one command to another is called pipe, you can search better to use other resources.

Browser other questions tagged

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