python中可以使用os.system()方法操作shutdown命令來設(shè)置電腦自動關(guān)機。
實現(xiàn)代碼如下:
#-*-coding:utf-8-*-
"""
CreatedonMonNov1919:56:072018
@author:10091
"""
fromdatetimeimportdatetime,time,date
importos
tmNow=datetime.now()
d=date.today()
#設(shè)置關(guān)閉時間為今天20:26:00
t=time(20,26,0)
shtdownTime=datetime.combine(d,t)
defShutDown():
whileTrue:
tmNow=datetime.now()
timedDelta=(shtdownTime-tmNow).total_seconds()
iftimedDelta<60:
print'還有59s關(guān)機,趕快保存一下!'
os.system('shutdown-s-f-t30')
#break
time.sleep(20)
else:
continue
if__name__=='__main__':
ShutDown()
shutdown命令:
shutdown[-i|-l|-s|-r|-a][-f][-m\computername][-txx][-c"
mment"][-dup:xx:yy]
參數(shù)介紹:
-i顯示GUI界面,必須是第一個選項
-l注銷(不能與選項-m一起使用)
-s關(guān)閉此計算機
-r關(guān)閉并重啟動此計算機
-a放棄系統(tǒng)關(guān)機
-m\computername遠程計算機關(guān)機/重啟動/放棄
-txx設(shè)置關(guān)閉的超時為xx秒
-c“comment”關(guān)閉注釋(127個字符)
-f強制運行的應(yīng)用程序關(guān)閉而沒有警告
以上內(nèi)容為大家介紹了python3怎么寫自動關(guān)機?希望對大家有所幫助,如果想要了解更多Python相關(guān)知識,請關(guān)注IT培訓(xùn)機構(gòu):千鋒教育。