#!/usr/bin/env python
import os import time
def fun(name): write_name="command %s failed!\n" % name print write_name f = open('/tmp/cs.log','a') f.write(write_name) f.close()
def tary(name): print "the command is %s" % name command_id = os.system(name) while command_id != 0: fun(name) time.sleep(10) command_id = os.system(name)
time.sleep(5)
tary("reboot")
Python 编程中使用 time 模块可以让程序休眠
具体方法是time.sleep(秒数),其中“秒数”以秒为单位,可以是小数,0.1秒则代表休眠100毫秒。
# 例1:循环输出休眠1秒 import time i = 1 while i <= 3: print i # 输出i i += 1 time.sleep(1) # 休眠1秒 # 例1:循环输出休眠100毫秒 import time i = 1 while i <= 3: print i # 输出i i += 1 time.sleep(0.1) # 休眠0.1秒
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有