zoukankan      html  css  js  c++  java
  • sys.stdout.write与sys.sterr.write(三)

    目标:

      1.使用sys.stdout.write模拟"|"的顺时针变化— | / 

      2.使用sys.stderr.write模拟"|"的顺时针变化— | /

    1.sys.stdout.write模拟"|"的顺时针变化

    代码如下:

    [root@localhost python]# cat animation.py

    #!/usr/bin/env python
    #coding:utf8
    
    import sys,time
    
    for i in xrange(10):
        for ch in '-\|/':
            print '%-20s%s
    ' % ('waiting...', ch)
            #sys.stdout.write('%-20s%s
    ' % ('waiting...', ch))
            sys.stdout.flush()
            time.sleep(0.2)

    •运行代码,测试效果

    [root@localhost python]# python animation.py

    waiting...          -
    waiting...          
    waiting...          |
    waiting...          /
    waiting...          -
    waiting...          
    waiting...          |
    waiting...          /
    waiting...          -
    waiting...          
    waiting...          |
    waiting...          /
    waiting...          -
    waiting...          
    waiting...          |
    waiting...          /
    waiting...          -
    waiting...          
    waiting...          |
    waiting...          /
    waiting...          -
    waiting...          
    waiting...          |
    waiting...          /
    waiting...          -
    waiting...          
    waiting...          |
    waiting...          /
    waiting...          -
    waiting...          
    waiting...          |
    waiting...          /
    waiting...          -
    waiting...          
    waiting...          |

    *提示:上述代码使用print是为了打印代码的过程,将print代码行注释,打开sys.stdout.write代码行,重新运行,即可看到"|"顺时针变化的效果。

    2.sys.stderr.write模拟"|"的顺时针变化

    代码如下:

    [root@localhost python]# cat animation.py

    #!/usr/bin/env python
    #coding:utf8
    
    import sys,time
    
    for i in xrange(10):
        for ch in '-\|/':
            #print '%-20s%s
    ' % ('waiting...', ch)
            sys.stderr.write('%-20s%s
    ' % ('waiting...', ch))
            #sys.stdout.flush()
            time.sleep(0.2)

    •运行代码,测试效果

    *提示:使用sys.stderr.write没有缓存,注释sys.stdout.flush()

  • 相关阅读:
    Jasper 常用知识点总结
    Linux
    搭建spring项目,无法创建RequestMappingHandlerMapping异常
    pom.xml文件设置
    MySQL的常用JSON函数
    SQL中的条件判断语句(case when zhen if,ifnull)用法
    sql查询原理
    sql积累
    Linux常用命令大全
    mysql中group by 的用法解析
  • 原文地址:https://www.cnblogs.com/xkops/p/6247732.html
Copyright © 2011-2022 走看看