简体   繁体   中英

Saving output in txt file

I have a python program that gives 3000 outputs in five seconds, and it gives millions of outputs in minutes. So I wanna know how to save the outputs in a '.txt' file automatically.

I think I need a python program to save all my outputs in a.txt file. I use python 2 for some reason.

The problem I have is,

  • windows does not have unlimited scrollback.
  • I don't have ubuntu.
  • If I try to copy my outputs my system hangs cuz it has just 4gb ram.

Help me guys, Thanks in advance.

By output I am assuming you are talking about print() statements. You can redirect your standard output to have print() go to a file instead:

import sys

with open('output.txt','w') as f:
    sys.stdout = f
    print('Hello')

Add this piece of code before you start outputting:

f = open(“file.txt”, “w+”)

Add this piece of code during outputting:

f.write(outputvar)

Add this piece of code after outputting:

f.close()

I hope this helps, if it doesn't, please tell me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM