Python - 在“终端”上的标准输出上打印

发布于 2024-07-25 03:08:52 字数 454 浏览 6 评论 0原文

在开始之前,我请大家为这个问题道歉。 也许这很愚蠢,但我找不到解决方案。 我正在远程计算机上工作,不知道是什么类型。

我的 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

谢谢 !

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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮倦 2024-08-01 03:08:52
import sys
print "Hi!"
sys.stdout.flush()
import sys
print "Hi!"
sys.stdout.flush()
下壹個目標 2024-08-01 03:08:52

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

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')
鹿! 2024-08-01 03:08:52

这是一个大胆的猜测,但查看评论中的措辞表明它可能是一个 Web 服务器应用程序(提示:有关您的环境的更多详细信息会有所帮助)。 在这种情况下,标准输出可能会去其他地方,至少不会去浏览器。

您实际上是如何运行该代码的? 您是在 shell 提示符下输入“python myprogram.py”,还是在浏览器中点击“重新加载”?

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?

淡忘如思 2024-08-01 03:08:52

除了 Jori 重载 sys.stdin 和 stdout 之外,存储 stdin 和 stdout 的原始值也是明智的,如下所示:

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

另一种简单的方法是:

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

如果你不这样做,你可能会发现自己摸不着头脑为什么您的打印功能不再将文本发送到屏幕。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文