How to save python screen output to a text file

前端 未结 10 1030
星月不相逢
星月不相逢 2020-12-08 06:16

I\'m new to Python. I need to query items from a dict and save the result to a text file. Here\'s what I have:

import json
import exec.fullog as e

input = e         


        
10条回答
  •  情书的邮戳
    2020-12-08 06:54

    We can simply pass the output of python inbuilt print function to a file after opening the file with the append option by using just two lines of code:

    with open('filename.txt', 'a') as file:
        print('\nThis printed data will store in a file', file=file)
    

    Hope this may resolve the issue...

    Note: this code works with python3 however, python2 is not being supported currently.

提交回复
热议问题