subprocess.popen() 的实时输出,而不是逐行输出

发布于 2024-10-10 22:15:14 字数 600 浏览 4 评论 0原文

我目前正在用 python 重写一个我曾经用 C++ 编写的小包装程序。它从文件中提取文件并将它们以另一种格式装箱。

在 C++ 中,我需要运行的系统命令的输出是“实时”的,即状态栏和某些命令的百分比指示器实时显示。使用 python,我将每个“百分比”单独转储到屏幕上(因为我逐行读取它)。这是一个例子:这就是 python 版本中状态栏的样子(一直持续到 100)。但在 C++ 中它确实会自我更新。

| (02/100)\rImporting AVC-H264: |                    | (03/100)\rImporting AVC-H264: |                       | (04/100)\rImporting AVC-H264: |=

这就是相应的 python 代码:

p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for line in iter(p.stdout.readline, ""):
   print line,

关于如何使它看起来像 C++ 的任何想法?

I'm currently rewriting a little wrapper program in python that I once wrote in C++. It extracts files from a file and boxes them in another format.

In C++ the output from the system commands I need to run was "real time" i.e the status bar and the percentage indicator of some commands where shown in real time. With python I get each 'percent' dumped on the screen individually (because I read it line by line). Here's an example: Thats how a status bar looks in the python version (this goes on until 100). In C++ it does update itself though.

| (02/100)\rImporting AVC-H264: |                    | (03/100)\rImporting AVC-H264: |                       | (04/100)\rImporting AVC-H264: |=

Thats the corresponding python code:

p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for line in iter(p.stdout.readline, ""):
   print line,

Any ideas on how I could make it look like in C++ ?

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

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

发布评论

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

评论(3

守不住的情 2024-10-17 22:15:14

可能有两件事......

readline 可能正在改变你的输出的一些东西
程序。我相信 \r 是回车符并告诉终端
返回到开头
行,然后程序可以输出覆盖刚刚输出的文本。
Readline 很可能会删除这个。

首先要尝试的是,

p = subprocess.Popen(args, stdout=subprocess.PIPE, \
                     stderr=subprocess.PIPE, \
                     universal_newlines=True)
for line in iter(p.stdout.readline, ""):
    sys.stdout.write('\r'+line[:-1])
    sys.stdout.flush()

必须进行刷新,因为标准输出会缓冲,直到它获得 \n
当然你不会写一个。

Could be two things...

It's likely that readline is changing some things from the output of your
program. I believe \r is carriage return and tells the terminal
to return to the beginning of the
line and then the program can output overtop the text it just output.
Readline is most likely removing this.

First thing to try,

p = subprocess.Popen(args, stdout=subprocess.PIPE, \
                     stderr=subprocess.PIPE, \
                     universal_newlines=True)
for line in iter(p.stdout.readline, ""):
    sys.stdout.write('\r'+line[:-1])
    sys.stdout.flush()

You have to do the flush because stdout buffers until it gets a \n and of
course you're not writing one.

﹂绝世的画 2024-10-17 22:15:14

在 Windows 上,您可以输出退格字符(ASCII 代码 8)。示例(仅使用一位数字打印当前迭代):

>>> import time
>>> import sys
>>> for i in xrange(10):
...     sys.stdout.write(str(i))
...     time.sleep(.5)
...     sys.stdout.write(chr(8))
...

您需要跟踪当前行上的字符数......我确信一定有更好的方法。

在 Linux 上,您似乎可以编写回车符来重置光标位置。请参阅擦除当前打印的控制台行终端或控制台中的就地进度输出

On Windows, you can output the backspace character (ASCII code 8). Example (prints the current iteration using only single digit numbers):

>>> import time
>>> import sys
>>> for i in xrange(10):
...     sys.stdout.write(str(i))
...     time.sleep(.5)
...     sys.stdout.write(chr(8))
...

You would need to keep track of the number of characters on the current line... I'm sure there must be a better way.

On Linux, it appears that you can write carriage returns to reset the cursor position. See Erase the current printed console line and In-place progress output in the terminal or console.

迎风吟唱 2024-10-17 22:15:14

抱歉,但我不太了解实时部分,但也许我可以帮助“自我更新”部分,试试这个:

for line in iter(p.stdout.readline, ""):
   print line + '\r',

Sorry but i didn't understand well the real time part, but maybe i can help with "update it self" part, try this:

for line in iter(p.stdout.readline, ""):
   print line + '\r',
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文