3

I have a large python script. It prints a bunch of output on my terminal console. The problem is the print is not happening altogether. Some print statements print one blob of statements together, then under that some other part of code prints some stuff. It goes on as long as the main loop runs.

Issue is I get the output as I want but all is getting printed on console as that is where we are running the python main script.

It would be very helpful if along with the print happening at console, I can get all the output in console in same format to a text file also for retention.

Again, there are bunch of print statements occurring in different parts of the whole script. So not sure how to retain the whole output of console in same format to a final text file.

11
  • what system do you use? Commented Jul 15, 2020 at 7:37
  • Currently running on mac terminal. But in the end the script wud run on linux shell on a server. Script is written in python Commented Jul 15, 2020 at 7:39
  • 2
    python -u my.py | tee my_file.txt Commented Jul 15, 2020 at 7:39
  • will that print the output on console too? Commented Jul 15, 2020 at 7:40
  • Have you ever thought about logging? Commented Jul 15, 2020 at 7:40

4 Answers 4

3

I would rather go ahead with bash and use tee command. It redirects the output to a file too.

python -u my.py | tee my_file.txt

Sign up to request clarification or add additional context in comments.

1 Comment

how can we do this in the script. Thing is I want to take away any more addition by a user. I want them to just run the script and pass some argument. It shows the output and also stores the same in a file on a directory. The code would name the file accordingly w/o them thinking what the name of file to keep. So something which tee does but inside the script
3

If you want to do the redirection within the Python script, setting sys.stdout to a file object does the trick:

import sys
sys.stdout = open('file', 'w')
print('test')

A far more common method is to use shell redirection when executing (same on Windows and Linux):

$ python foo.py > file

Check this thread Redirect stdout to a file in Python?

Custom Print function for both console and file, replace all print with printing in the code.

outputFile = open('outputfile.log', 'w')

def printing(text):
    print(text)
    if outputFile:
        outputFile.write(str(text))

4 Comments

will that also print the output on console? I want the output on console and also the same output copied to a file w/o having to do redirection in shell, but just in script
Ok I tried this. It is good, but it then doesn't print the output on console and sends it only to file. How can we have both w/o a user having to add more extra commands at console
I did sys.stdout to file. It prints to file. How does one go back to console format. It doesnt print anything to console anymore. I tried adding sys.__stdout__. No result
you can create a custom function for print and replace it with all prints
2

If your python script is file.py, Then use :

python3 file.py > output.txt

Or

python file.py > output.txt

Depending on your python version. This statement (>) will all the outputs of the program into the stdout to the file, output.txt

EDIT :

python3 file.py > output.txt;cat output.txt

The above line can be used to print the file output.txt after the program execution.

EDIT2 : Another possible option to use a custom print function :

f = open('output.txt')
def custom_print(e = '\n',*s)
    for i in s[:-1]:
        print(i,end=' ')
    print(s[-1],end = e)
    f.write(s)
#Your code
#
f.close()

9 Comments

it might only send it to txt. How to also see the same output on console
python file.py > output.txt;cat output.txt Will also print the file to console, if you want it to print it along with the interpreter running, you might need to use a print to file and a print to console function.
Edited, check the answer again.
ah get it. Is there a way to do the same in script and just run the script in terminal? Issue is there are a bunch of command line argument that wud be passed while running the script. I don't want to complicate it by having extra add on in the end as above
what is s? Do I hv to write this custom_print every time where i hv a print?
|
1

you have to add file argument to the print() function

print('whatever', file = file_name)

8 Comments

wud I have to add this to all print statements? I have like a tonne of such prints. I also want the output to be printed in console too.
@Baktaawar yes you would have to add to all. If you are using the Pycharm it is very easy. It will be printed and saved to a file
@Baktaawar there is also one way sys.stdout = open('file_name'', 'a')
using VScode.. currently testing the code on jupyter. But eventually code has to be run as a script in console. Quick check- will this adding of file to all print also print the output to console as usual
is this sys.stdout only needed once at the top? Or after each print statement
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.