python程序执行完不自动退出

727次阅读
没有评论

python程序执行完不自动退出

默认情况下,python执行完成后会直接退出。如果还想查看运行结果或是接着执行其他命令就不行了。遇到这种情况,可以在执行脚本的命令中添加 -i 选项,例如我想执行example.py:

> python -i example.py

这样,在执行完example.py 后,程序就会停留在python的控制台了。

当然也可以在脚本里加一个输入函数让程序执行停下来,可以根据需要选择。

简单的方法是在最后加上如下语句:

 os.system("pause")

__author__ = 'di_shen_sh'
# coding=utf8
# 上句说明使用utf8编码
 
try:
    import os
    import sys
    import time
    #关键语句,使得py文件能够找到其他module
    #关键语句,使得py文件能够双击在外部运行
    sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    from norlib_python.Mail import HtmlMail
    from norlib_python.Socket import Ip
except Exception,ex:
    print 'Load library Exception:\r\n'
    print ex
    os.system("pause")
 
#SendComputerRebootMail
 
if __name__ == "__main__":
    try:
        ips = Ip.GetIps()
        strIps = '<br/>'.join(ips)
        datetime = time.localtime(time.time())
        strDateTime = time.strftime('%Y-%m-%d %H:%M:%S',datetime)
        content = u'Info of Reboot Server<br/>DateTime:<br/>%s<br/>Ip:<br/>%s<br/>' % (strDateTime,strIps)
        subject = u'计算机重启事件'
        msg = HtmlMail.CreateMessage(subject,content)
        HtmlMail.Send("smtp.163.com","abc@163.com","123",msg,"abc@163.com","abc@163.com")
    except Exception,ex:
        print 'Exception:\r\n'
        print ex
    finally:
        os.system("pause")
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2019-11-10发表,共计1309字。
新手QQ群:570568346,欢迎进群讨论 Python51学习
评论(没有评论)