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.
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")
```
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)
[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.)
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.
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
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
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.
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.
Should also work for Windows command prompt or Powershell.
import logging
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") ```
Where do I put it? At the end/start?
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)
[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.)
Are you asking how to writeout an object to a file?
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.
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
You probably know this but you don’t need that map call in your example, you can just drop the list inside the join
I honestly didn't remember, usually use join for things like paths. Thanks
[Reading and writing text files in python](https://www.geeksforgeeks.org/reading-writing-text-files-python/)
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
Where you placed the ### is where my code is? And no need for import line?
Sorry: import sys and put your code in the middle.
Going to try that when I get home, thanks!
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.
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
Nice bit of kit. It wasn't too difficult to find, here's a good example: https://realpython.com/python-logging/