简体   繁体   English

附加到上一行

[英]Append to previous line

I'm doing folder/file processing in Python. 我正在用Python处理文件夹/文件。 To notify the user, I output some messages to the console. 为了通知用户,我将一些消息输出到控制台。 These messages look similar to this: 这些消息类似于以下内容:

Creating folders...
DONE
Creating files...
DONE
All done!

This is fine if the process is short, but in my case, the process (and the messages) is not short. 如果过程很短,那很好,但就我而言,过程(和消息)并不短。 I don't want to waste new lines on the success/fail messages. 我不想在成功/失败消息上浪费新的一行。 I want them to look like this: 我希望他们看起来像这样:

Creating folders... DONE
Creating files... DONE
All done!

The trick is to have "DONE" strings append to previous line after the specific task is completed. 诀窍是在完成特定任务后,将“ DONE”字符串附加到上一行。 So, first I just see: 所以,首先我只看到:

Creating folders...

and when the task is done, it becomes: 任务完成后,它将变为:

Creating folders... DONE

and moves on to the next task. 并继续执行下一个任务。 I've tried not ending the lines, but it didn't work: 我试过不结束行,但不起作用:

print("Creating folders... ", end="")
time.sleep(2) # fake process
print("DONE")
time.sleep(1)
print("Creating files... ", end="")
time.sleep(2)
print("DONE")

Well, it works, but both strings (task... result) show up at the same time (after the task is done). 很好,它可以工作,但是两个字符串(任务...结果)同时显示(任务完成后)。 I don't see the transition I've mentioned above. 我看不到上面提到的过渡。

I've found another way, move to the start of the line and replace the string: 我找到了另一种方法,移至行的开头并替换字符串:

print("Creating folders... ", end="\r")
time.sleep(2) # fake process
print("Creating folders... DONE")
time.sleep(1)
print("Creating files... ", end="\r")
time.sleep(2)
print("Creating files... DONE")

This seems to produce the desired effect, but I'm repeating and extending the previous message. 这似乎产生了预期的效果,但是我要重复并扩展前面的消息。 I'd rather just output the result, not repeat the task message again. 我只想输出结果,而不是再重复一次任务消息。

Is there a simpler solution to this? 有没有更简单的解决方案?


Also, why doesn't the first method I've tried work? 另外,为什么我尝试的第一种方法都不起作用? I print a text, and not end the line. 我打印文本,但不结束一行。 After some time I add another text and this gets appended to the previous line, because there's no line-break. 一段时间后,我添加了另一个文本,由于没有换行符,因此将其附加到上一行。 Since there's time difference between two prints, I should see the transition, but I don't. 由于两次打印之间存在时间差异,因此我应该看到过渡,但是我没有。 They get printed at the same time. 它们同时打印。 Why is that? 这是为什么?

You need to flush the buffer after each print statement (that uses end="" ) in order to ensure that the messages are pushed to the console immediately. 您需要在每个打印语句(使用end="" )之后刷新缓冲区,以确保将消息立即推送到控制台。 See print() documentation. 请参阅print()文档。

Working Example with flush parameter of print function: 带有打印功能的flush参数的工作示例:

import time

print("Creating folders... ", end="", flush="True")
time.sleep(2)  # fake process
print("DONE")
time.sleep(1)
print("Creating folders... ", end="", flush="True")
time.sleep(2)
print("DONE")

Working Example with manual flushing: 手动冲洗的工作示例:

import time
import sys

print("Creating folders... ", end="")
sys.stdout.flush()
time.sleep(2) # fake process
print("DONE")
time.sleep(1)
print("Creating files... ", end="")
sys.stdout.flush()
time.sleep(2)
print("DONE")

See it in action! 观看实际操作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM