Python - 在“终端”上的stdout上打印

时间:2021-04-09 00:03:38

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 ?

我的python代码似乎有效,如下所示。问题是我试图在屏幕上打印一些输出,但没有任何反应。我已经尝试过print和raw_input但没有任何反应......你知道其他任何方法吗?

# 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 个解决方案

#1


4  

To redirect stdout to something that you can read, a file in this case:

要将stdout重定向到您可以读取的内容,在这种情况下是一个文件:

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')

#2


9  

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

#3


2  

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.

这是一个疯狂的猜测,但查看注释中的措辞表明它可能是一个Web服务器应用程序(提示:有关您的环境的更多详细信息将有所帮助)。在这种情况下,stdout可能会到达其他地方,至少不会到浏览器。

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?

你是如何实际运行该代码的?你在shell提示符下键入“python myprogram.py”,还是在浏览器中点击Reload?

#1


4  

To redirect stdout to something that you can read, a file in this case:

要将stdout重定向到您可以读取的内容,在这种情况下是一个文件:

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')

#2


9  

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

#3


2  

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.

这是一个疯狂的猜测,但查看注释中的措辞表明它可能是一个Web服务器应用程序(提示:有关您的环境的更多详细信息将有所帮助)。在这种情况下,stdout可能会到达其他地方,至少不会到浏览器。

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?

你是如何实际运行该代码的?你在shell提示符下键入“python myprogram.py”,还是在浏览器中点击Reload?