Can I get the print statement in pythonw?

pythonw.exe does not have a console, so I do not see print output. But my OKAY program in python.exe failed in pythonw.exe. I just want to see the logs from the python interpreter and the log printed by my print statement, is this doable?

+3
source share
4 answers

You can globally redirect stdout by assigning sys.stdout:

import sys
sys.stdout = open("mylog.txt", "w")

Then the rest of your stdout program, including print instructions, will go to the mylog.txt file.

+3
source

You can redirect print output to any stream. for instance

logfile = open("logfile.txt", "w")
print >>logfile, "This is a log test"

python logging.

+1

, .write. , sys.stdout, sys.stderr , , .

logControl = None
def LogWrite(text, color):
    logControl.SetDefaultStyle(wx.TextAttr(color, wx.NullColour))
    logControl.WriteText(text)

class RedirectStdOut:
    def write(self,string):
        LogWrite(string, "BLACK")

class RedirectStdErr:
    def write(self,string):
        LogWrite(string, "RED")

....

    sys.stdout = RedirectStdOut()
    sys.stderr = RedirectStdErr()

logControl - , . , , dbgview , .

0

, . . ,

    import time
    time.sleep(5)

Save it as test.pyw. When you run this program, it will appear in the task manager. Now change it by adding a print statement. This text file contains more than 100 lines.

    import time
    string = ''
    filename = 'pathto/text.txt'
    f = open(filename,'r')
    for line in f:
            string = string + ' ' + line
    f.close()
    print string
    time.sleep(5)

The program will fail.

-1
source

Source: https://habr.com/ru/post/1763951/


All Articles