Python - printing to stdout on a "terminal"

Before you begin, I ask you all to apologize for the question. It may be stupid, but I can not find a solution. I work on a remote machine and have no idea what type.

My python code that seems to work is below. The problem is that I'm trying to print some outputs on the screen, but nothing happens. I tried both print and raw_input, but nothing happens ... Do you know any other way to do this?

# Set up fields of reply message based on query
def prepareReply():
    global authorReply, authorReplyLen, localConvId, originConvId, blbContentAndUntUnz, linkName

    print "PLOP!"
    raw_input("blabla")

    #print "="*10

Thanks!

+3
source share
4 answers

To redirect stdout to what you can read, the file in this case:

class PyLogger:

  def __init__(self, source):
    self.file_handle = open('Python_Log.txt', 'a')
    self.source=source
    self.buf = []

  def write(self, data):
    self.buf.append(data)
    if data.endswith('\n'):
      self.file_handle = open('Python_Log.txt', 'a')
      self.file_handle.write('\t' * indent_level)
      self.file_handle.write(self.source + "::" + ''.join(self.buf))
      self.file_handle.close()
      self.buf = []

  def __del__(self):
    if self.buf != []:
      self.file_handle = open('Python_Log.txt', 'a')
      self.file_handle.write('\t' * indent_level)
      self.file_handle.write(self.source + "::" + ''.join(self.buf) + '\n')
      self.file_handle.close()      
    self.file_handle.close()

import sys
sys.stdout = PyLogger('stdout')
sys.stderr = PyLogger('stderr')
+4
source
import sys
print "Hi!"
sys.stdout.flush()
+9
source

, , , , - (: ). stdout, , - , , .

How do you really use this code? Do you type "python myprogram.py" at the command line, or do you click "Update" in your browser?

+2
source

In addition to overloading Jori sys.stdin and stdout, it is also advisable to store the original stdin and stdout values ​​as follows:

import sys
savestreams = sys.stdin, sys.stdout
sys.stdout = PyLogger('stdout')
sys.stderr = PyLogger('stderr')
... 
...
...
sys.stdin, sys.stdout = savestreams

Another easy way:

sys.stdout = sys.__stdout__
sys.stdin = sys.__stdin__

if you don’t, you can scratch your head why your print function no longer sends text to the screen.

0
source

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


All Articles