• 技术文章 >常见问题 >Python常见问题

    python3怎么写自动关机?

    yangyang2020-05-22 16:18:53原创4495

    python中可以使用os.system()方法操作shutdown命令来设置电脑自动关机。

    实现代码如下:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    # -*- coding: utf-8 -*-

    """

    Created on Mon Nov 19 19:56:07 2018

     

    @author: 10091

    """

     

    from datetime import datetime,time,date

    import os

     

     

    tmNow = datetime.now()

    d = date.today()

    #设置关闭时间为今天20:26:00

    t = time(20,26,0)

    shtdownTime = datetime.combine(d,t)

    def ShutDown():

         while True:

             tmNow = datetime.now()

             timedDelta = (shtdownTime - tmNow).total_seconds()

             if timedDelta < 60:

                 print '还有59s关机,赶快保存一下!'

                 os.system('shutdown -s -f -t 30')          

                 #break

                 time.sleep(20)

             else:

                 continue

                  

    if __name__ == '__main__':

         ShutDown()

    shutdown命令:

    1

    2

    shutdown [-i | -l | -s | -r | -a] [-f] [-m \computername] [-t xx] [-c "   

        mment"] [-d up:xx:yy]

    参数介绍:

    更多Python知识请关注Python自学网

    专题推荐:python3
    上一篇:python安装以后如何运行? 下一篇:python3中%d什么意思?

    相关文章推荐

    • windows10怎么把python添加到路径• 如何用Python代码写出音乐?• 如何用Python进行抽样?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网