默認情況下,python執(zhí)行完成后會直接退出。如果還想查看運行結(jié)果或是接著執(zhí)行其他命令就不行了。遇到這種情況,可以在執(zhí)行腳本的命令中添加-i選項,例如我想執(zhí)行example.py:
>python-iexample.py
這樣,在執(zhí)行完example.py后,程序就會停留在python的控制臺了。
當然也可以在腳本里加一個輸入函數(shù)讓程序執(zhí)行停下來,可以根據(jù)需要選擇。
簡單的方法是在最后加上如下語句:
os.system("pause")
__author__='di_shen_sh'
#coding=utf8
#上句說明使用utf8編碼
try:
importos
importsys
importtime
#關鍵語句,使得py文件能夠找到其他module
#關鍵語句,使得py文件能夠雙擊在外部運行
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
fromnorlib_python.MailimportHtmlMail
fromnorlib_python.SocketimportIp
exceptException,ex:
print'LoadlibraryException:\r\n'
printex
os.system("pause")
#SendComputerRebootMail
if__name__=="__main__":
try:
ips=Ip.GetIps()
strIps='
'.join(ips)
datetime=time.localtime(time.time())
strDateTime=time.strftime('%Y-%m-%d%H:%M:%S',datetime)
content=u'InfoofRebootServer
DateTime:
%s
Ip:
%s
'%(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")
exceptException,ex:
print'Exception:\r\n'
printex
finally:
os.system("pause")
以上內(nèi)容為大家介紹了python培訓之程序執(zhí)行完不自動退出,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構(gòu):千鋒教育。