python 自动重启本程序

2165阅读 0评论2011-08-26 jian_g_
分类:

#coding=utf8
import time
import sys
import os
def restart_program():
    """Restarts the current program.
    Note: this function does not return. Any cleanup action (like
    saving data) must be done before calling this function."""
    python = sys.executable
    os.execl(python, python, * sys.argv)
if __name__ == "__main__":
    print 'start...'
    answer = raw_input("Do you want to restart this program ? ")
    if answer.strip() in "y Y yes Yes YES".split():
        restart_program()
    print "3秒后,程序将结束..."
    time.sleep(3)
 
 
运行结果如下:
[root@localhost sinatpy2.x]# python restart_self.py
start...
Do you want to restart this program ? y
start...
Do you want to restart this program ? y
start...
Do you want to restart this program ? y
start...
Do you want to restart this program ? y
start...
Do you want to restart this program ? yes
start...
Do you want to restart this program ? n
3秒后,程序将结束...
[root@localhost sinatpy2.x]#

上一篇:分区,格式化,挂载
下一篇:常用hash算法介绍及实现源码