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 !
import sys
print "Hi!"
sys.stdout.flush()
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?
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')
In addition to Jori's overloading of sys.stdin and stdout, it is also wise to store the original values of stdin and stdout as follows:
import sys
savestreams = sys.stdin, sys.stdout
sys.stdout = PyLogger('stdout')
sys.stderr = PyLogger('stderr')
...
...
...
sys.stdin, sys.stdout = savestreams
Another simple way is:
sys.stdout = sys.__stdout__
sys.stdin = sys.__stdin__
if you don't do this, you may find yourself scratching your head as to why your print function no longer sends text to your screen.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.