JiFu's Wiki JiFu's Wiki
首页
  • HTML
  • JavaScript
  • NodeJS
  • Vuejs
  • 微信小程序
  • Python
  • 数据库
  • 中间件
  • 算法
  • 软件工程
  • Wordpress
  • iOS开发
  • Android开发
  • Linux
  • Windows
  • MacOS
  • Docker
  • Vim
  • VSCode
  • Office
  • 其他
  • Photoshop
  • Sketch
  • Mac
  • 游戏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
首页
  • HTML
  • JavaScript
  • NodeJS
  • Vuejs
  • 微信小程序
  • Python
  • 数据库
  • 中间件
  • 算法
  • 软件工程
  • Wordpress
  • iOS开发
  • Android开发
  • Linux
  • Windows
  • MacOS
  • Docker
  • Vim
  • VSCode
  • Office
  • 其他
  • Photoshop
  • Sketch
  • Mac
  • 游戏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 数据库

  • Python

    • Python介绍
    • 国内pip更换豆瓣的pypi源
    • sys.stdout.write实现Python控制台实时刷新打印
      • 我们先来看看Print方法打印的效果
      • help查看
      • 清屏操作
      • 方法1
      • 方法2
      • 进度条的特点
      • 实现
    • 发布你自己的轮子 - PyPI打包上传实践
    • Seaborn库绘制了17个超好看图表
    • Python脚本打包exe Auto-py-to-exe
    • Build-in

    • Flask

    • Libaray

  • 中间件

  • 算法

  • 软件工程

  • Wordpress

  • 后端技术
  • Python
JiFu
2023-09-13
目录

sys.stdout.write实现Python控制台实时刷新打印

# 前言

# 我们先来看看Print方法打印的效果

from datetime import datetime as dt
import sys
import time

for i in range(5):
    print(dt.now())
    time.sleep(1)
1
2
3
4
5
6
7

# 输出结果

C:\Users\Administrator\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/untitled/test.py
2018-08-06 16:46:46.636256
2018-08-06 16:46:47.636313
2018-08-06 16:46:48.636370
2018-08-06 16:46:49.636427
2018-08-06 16:46:50.637484

Process finished with exit code 0
1
2
3
4
5
6
7
8

可以看到,用print打印出来自动换行且不会清除上一个结果

# help查看

help(print)
1

# 输出结果

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
可以看到end=“\n”表示了print自带换行
1
2
3
4
5
6
7
8
9
10

如果我想要在一行中打印一串信息,并且在下一次执行的时候删除这一行再重新打印(效果类似如此),该如何做呢?尝试清屏可不可以?

# 清屏操作

清屏试一试,查阅别的博客的方法有如下代码:

import os

os.system('cls')
1
2
3

但这是在命令行里使用的,用在编译器里不行。

# 解决办法

这时候就要用到sys.stdout.write了

# 方法1

from datetime import datetime as dt
import sys
import time


while True:
    a = dt.now()
    sys.stdout.write("\r{0}".format(a))
    sys.stdout.flush()
    time.sleep(1)
1
2
3
4
5
6
7
8
9
10

# 方法2

from datetime import datetime as dt
import sys
import time


for i in range(20):
    a = dt.now()
    sys.stdout.write("\r{0}".format(a))
    sys.stdout.flush()
    sys.stdout.write('\033[4A')
    time.sleep(1)
1
2
3
4
5
6
7
8
9
10
11

其关键就在于使用'\r'这个转义字符(回到行首), sys.stdout.write首先打印这一行后不带任何结尾(前文已经说过print打印结尾带end="\n",表示自带换行,换行了就不能在对已经打印的这一行进行更改编辑),使用了转移字符"\r"使得光标回到行首,再把缓冲区显示出来,就得到了我们所需要的效果。

C:\Users\Administrator\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/untitled/test.py
2018-08-06 18:26:21.264878
1
2

Run只会显示这一个,并且一秒钟更新一次。

# 进度条实现

# 进度条的特点

  • 有标刻度显示所占总进度比例

  • 有百分比显示所占比例

# 实现

import time,sys

for i in range(100):
    percent = i / 100
    sys.stdout.write("\r{0}{1}".format("|"*i , '%.2f%%' % (percent * 100)))
    sys.stdout.flush()
    time.sleep(1)
1
2
3
4
5
6
7

# 输出效果

|||||||||||||||||||||||||||||||||33.00%

1
2
上次更新: 2024/08/11, 01:59:03
国内pip更换豆瓣的pypi源
发布你自己的轮子 - PyPI打包上传实践

← 国内pip更换豆瓣的pypi源 发布你自己的轮子 - PyPI打包上传实践→

最近更新
01
Disable notification "to get future google chrome updates you'll need macos 10.13 or later" on mac
05-14
02
MacOS软件推荐
04-30
03
Debian Sway开发机安装手册
03-26
更多文章>
Theme by Vdoing | Copyright © 2019-2025 Ji Fu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式