为了账号安全,请及时绑定邮箱和手机立即绑定

如何在控制台上的相同位置写入输出?

如何在控制台上的相同位置写入输出?

慕工程0101907 2019-08-30 16:41:46
我是python的新手,我正在编写一些脚本来自动从FTP服务器等下载文件。我想显示下载的进度,但我希望它保持在相同的位置,例如:输出:正在下载文件FooFile.txt [47%]我试图避免这样的事情:     Downloading File FooFile.txt [47%]     Downloading File FooFile.txt [48%]     Downloading File FooFile.txt [49%]我该怎么做呢?
查看完整描述

3 回答

?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

您还可以使用回车:


sys.stdout.write("Download progress: %d%%   \r" % (progress) )

sys.stdout.flush()


查看完整回答
反对 回复 2019-08-30
?
慕妹3242003

TA贡献1824条经验 获得超6个赞

使用像curses模块这样的终端处理库:


curses模块提供了curses库的接口,这是便携式高级终端处理的事实标准。


查看完整回答
反对 回复 2019-08-30
?
手掌心

TA贡献1942条经验 获得超3个赞

Python 2

我喜欢以下内容:


print 'Downloading File FooFile.txt [%d%%]\r'%i,

演示:


import time


for i in range(100):

    time.sleep(0.1)

    print 'Downloading File FooFile.txt [%d%%]\r'%i,

Python 3

print('Downloading File FooFile.txt [%d%%]\r'%i, end="")

演示:


import time


for i in range(100):

    time.sleep(0.1)

    print('Downloading File FooFile.txt [%d%%]\r'%i, end="")


查看完整回答
反对 回复 2019-08-30
  • 3 回答
  • 0 关注
  • 398 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信