Tutorial :Python - Print on stdout on a “terminal”



Question:

Before starting, I ask you all to apologize for the question. Maybe it is stupid, but I cannot find a solution. I am working on a remote machine, and have no idea what type.

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

# 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 !


Solution:1

To redirect stdout to something that you can read, a 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')  


Solution:2

import sys  print "Hi!"  sys.stdout.flush()  


Solution:3

This is a wild guess, but looking at the wording in your comments indicates that it might be a web server application (hint: more detail on your environment would be helpful). In this case, stdout is probably going somewhere else, at least not to the browser.

How are you actually running that code? Do you type "python myprogram.py" at a shell prompt, or do you hit Reload in your browser?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »