在X小时内关闭计算机

2024-05-16 01:51:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,正在尝试编写一个程序。我想这个程序问我是要关机,还是让我的电脑进入睡眠状态。然后问我希望在多长时间内完成这个动作。在

我想创建这个,这样当我在床上听音乐或播客时,我不会整晚都开着电脑。到目前为止,我已经得到它来问我是否想睡觉/关机以及多长时间。我不确定在x时间内关闭计算机的语法是什么。如果有人能告诉我怎么做,我将不胜感激!在

option = input('Enter "SD" to Shutdown or "S" to sleep: ') if option == "SD" or option == "sd": print('You chose to Shutdown') time = input('In how many hours would you like to shutdown the PC? ') # os.system("shutdown /p") elif option == "S" or option == "s": print('You chose to sleep') time = input('In how many hours would you like to put the PC to sleep? ')


Tags: orto程序youinputtimesleepsd
1条回答
网友
1楼 · 发布于 2024-05-16 01:51:48

您可以使用以下方法在Windows上设置关机超时:

shutdown /t xxx

将关机前的超时时间设置为xxx秒。在

The valid range is 0-315360000 (10 years), with a default of 30. If the timeout period is greater than 0, the /f parameter is implied.

此外,您还可以始终使用以下命令查找其他选项:

^{pr2}$

在您的代码中:

os.system("shutdown /t {}".format(time))

相关问题 更多 >