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 with
open(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.
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?
– wensiso
Could you explain your idea better? It’s not very clear to me what you want
– absentia