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]# vim railway.py

    [root@localhost python]# cat railway.py
    #!/usr/bin/env python
    #coding:utf8
    
    import sys,time
    
    width = 20
    i = 0
    while True:
        sys.stdout.write('%s%s%s
    ' %('#' * i, 'a', '#' * (width - i)))
        i +=1
        sys.stdout.flush()
        time.sleep(0.2)
        if i > 
            i = 0
        #打印出每行的过程,注释print会出现a字符从首字符跑到末尾,然后继续重复上面的工作,及模拟火车道的轨迹。
        print

    •运行代码,测试效果

    [root@localhost python]# python railway.py

    a####################
    #a###################
    ##a##################
    ###a#################
    ####a################
    #####a###############
    ######a##############
    #######a#############
    ########a############
    #########a###########
    ##########a##########
    ###########a#########
    ############a########
    #############a#######
    ##############a######
    ###############a#####
    ################a####
    #################a###
    ##################a##
    ###################a#
    ####################a
    a####################
    #a###################
    ##a##################

    *提示:代码中的print是为了打印上面每一步的变化过程,将print注释重新运行,就会看到a从头到尾不断变化(在一行之中),实现模拟火车道轨迹变化的过程。

    2.sys.stderr.write模拟火车道轨迹变化(该代码注释了print)

    代码如下:

    [root@localhost python]# cat railway.py

    #!/usr/bin/env python
    #coding:utf8
    
    import sys,time
    
    width = 20
    i = 0
    while True:
        sys.stderr.write('%s%s%s
    ' %('#' * i, 'a', '#' * (width - i)))
        i +=1
        time.sleep(0.2)
        if i > 
            i = 0

    •运行代码,测试效果

  • 相关阅读:
    Validation failed for one or more entities
    sql 存储过程
    SQL Server分页3种方案比拼
    case when 用法
    C#如何计算代码执行时间
    透过 Jet.OLEDB 读取 Excel里面的数据
    DataBinding?资料系结?资料绑定?
    ASP.NET的OutputCache
    我想写程序#3 之 「简单地设计自己的数据表(Table)」
    我想写程序#1 之 「先确立志向」
  • 原文地址:https://www.cnblogs.com/xkops/p/6245989.html
Copyright © 2011-2022 走看看