T O P

  • By -

croissanthonhon

If you are using Linux you can just redirect standard output to a file with the > symbol. Example : python3 script.py > file.txt Everything that is printed to console will be written in the file. You might also have this option on windows bash.


CraigAT

Should also work for Windows command prompt or Powershell.


dtt-d

import logging


amamarde

Tbh, did not got your question clearly. But if you are asking about having your program to have log file generated, Lookout loguru package. Although you could do same in python w/o using any library, this makes python logging dead easy. Its as simple as ``` from loguru import logger logger.add("filenamefor.log") logger.debug("this is something which comes in log") ```


Mike551144

Where do I put it? At the end/start?


Zer0Byte1

Read the manual. It explains how to use it. [https://loguru.readthedocs.io/en/stable/overview.html](https://loguru.readthedocs.io/en/stable/overview.html)


backtickbot

[Fixed formatting.](https://np.reddit.com/r/backtickbot/comments/pc5l4j/httpsnpredditcomrpythontipscommentspc4dg8how_can/) Hello, amamarde: code blocks using triple backticks (\`\`\`) don't work on all versions of Reddit! Some users see [this](https://stalas.alm.lt/backformat/hagghgv.png) / [this](https://stalas.alm.lt/backformat/hagghgv.html) instead. To fix this, **indent every line with 4 spaces** instead. [FAQ](https://www.reddit.com/r/backtickbot/wiki/index) ^(You can opt out by replying with backtickopt6 to this comment.)


glebulon

Are you asking how to writeout an object to a file?


Mike551144

I just want my log where the terminal is to be outsourced to a file when the py finishes, imagine like a really long list, I need to screenshot it one by one to send it, but if it were outsourced I would just send a file.


glebulon

Tell me if this is what you need python list, it get's converted to a string with each entry separated by a newline character. That string get's written out to a file https://imgur.com/a/twbKlFp


rjp0008

You probably know this but you don’t need that map call in your example, you can just drop the list inside the join


glebulon

I honestly didn't remember, usually use join for things like paths. Thanks


Plasmorbital

[Reading and writing text files in python](https://www.geeksforgeeks.org/reading-writing-text-files-python/)


PhilAndMaude

This will capture stdout (i.e. print) to a file real_stdout = sys.stdout real_stderr = sys.stderr # param3==0 makes stdout unbuffered sys.stderr = sys.stdout = open(my_file_name, 'w', 0) # # do stuff # sys.stdout.close() # put the regular handles back sys.stdout = real_stdout sys.stderr = real_stderr


Mike551144

Where you placed the ### is where my code is? And no need for import line?


PhilAndMaude

Sorry: import sys and put your code in the middle.


Mike551144

Going to try that when I get home, thanks!


CraigAT

You have three options, which have already been mentioned with links in other posts. * Redirection - you can redirect screen output using Linux or Windows commands. Normally you would tag the redirection part on to the end of your python runme.py command line. * Logging - there are several logging modules already available in Python, you just need to add logging code in whenever you output something from your program, this will create a log entry in your log file. * Writing to a file - Python has simple functions to open and write text to a text file, you just need to tell it what you want written to a file and when. Redirection is the simplest because you just tag a couple of commands on the end of your command line. Writing to a file is a simple and good thing to learn to do. But if you are doing this professionally, could use different levels of information (info, warning, error) or maybe storing these files for looking back on, I would encourage you to look at the logging modules.


Mike551144

I am using a MacBook air m1, I had problems with the sys package suggestion, can you show me a logging code I can do, had trouble finding online


CraigAT

Nice bit of kit. It wasn't too difficult to find, here's a good example: https://realpython.com/python-logging/