以下是一种自动关机代码的示例,适用于 Windows、Linux、Mac 等操作系统:

import os
import platform
import time

def auto_shutdown(minutes):
    if platform.system() == "Windows":
        os.system(f"shutdown.exe /s /t {minutes*60}")
    elif platform.system() == "Linux":
        os.system(f"shutdown -h +{minutes}")
    elif platform.system() == "Darwin":
        os.system(f"osascript -e 'tell app \"System Events\" to shut down'")
    else:
        print("不支持的操作系统")

# 设置自动关机时间为30分钟
auto_shutdown(30)

此代码通过调用操作系统的命令来实现自动关机功能。代码中首先使用 platform 模块判断当前操作系统类型(Windows、Linux 还是 Darwin/Mac),然后根据不同的操作系统执行相应的关机命令。

在 Windows 上,使用 os.system 函数调用 shutdown.exe 命令,/s 参数表示关机,/t 参数表示延迟时间(单位为秒)。

在 Linux 上,使用 os.system 函数调用 shutdown 命令,-h 参数表示关机,+minutes 参数表示延迟时间(单位为分钟)。

在 Mac 上,使用 osascript 命令调用 AppleScript 来执行关机操作。

请注意,在使用此代码之前,请确保您具有足够的权限来执行关机操作,并且谨慎设置关机时间。

自动关机代码:Windows、Linux、Mac通用示例

原文地址: https://www.cveoy.top/t/topic/buq7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录