Python 3.x 如何安排计算机在特定时间关机?

Python 3.x 如何安排计算机在特定时间关机?,python-3.x,Python 3.x,我运行一个计算量很大的程序,它的输出需要花费很多时间。我想,如果我能在收到输出后关闭电脑可能是个好主意。所以我使用了下面的代码(顺便说一句,它很好用): 但现在我有另一个程序(IDM)同时运行,从凌晨2点一直运行到早上7点。 我的问题是,如何设计一个简单的程序,仅当电脑时间超过上午7点时才关闭电脑 有人能帮我吗?您可以检查当前时间,并在关机前循环到指定的小时,例如: import os import time def shutdown(threshold=7): while time.

我运行一个计算量很大的程序,它的输出需要花费很多时间。我想,如果我能在收到输出后关闭电脑可能是个好主意。所以我使用了下面的代码(顺便说一句,它很好用):

但现在我有另一个程序(IDM)同时运行,从凌晨2点一直运行到早上7点。
我的问题是,如何设计一个简单的程序,仅当电脑时间超过上午7点时才关闭电脑

有人能帮我吗?

您可以检查当前时间,并在关机前循环到指定的小时,例如:

import os
import time

def shutdown(threshold=7):
    while time.gmtime().tm_hour < threshold:
        time.sleep(300)  # wait 5 minutes
    os.system("shutdown /s /t 90")
相反

import os
import time

def shutdown(threshold=7):
    while time.gmtime().tm_hour < threshold:
        time.sleep(300)  # wait 5 minutes
    os.system("shutdown /s /t 90")
def shutdown(threshold=7):
    if time.gmtime().tm_hour >= threshold:
        os.system("shutdown /s /t 90")