我正在做一个小项目来了解有关python 2.7的更多信息。我做了一个关机计时器,我得到了所有设置的GUI,我只需要关闭Windows 8的命令.cmd命令是:shutdown / t xxx。
我尝试了以下内容:
import subprocess
time = 10
subprocess.call(["shutdown.exe", "/t", "time"])
import os
time = 10
os.system("shutdown /t %s " %str(time))
两者都不起作用。 任何帮助表示赞赏,我使用Windows 8,所以我认为Windows 7的解决方案是不同的。
感谢您的回答,这是我做的关机计时器:
答案 0 :(得分:3)
subprocess.call
的第一个参数应该是程序参数序列(字符串)或单个字符串。
请尝试以下操作:
import subprocess
time = 10
subprocess.call(["shutdown.exe", "/t", str(time)]) # replaced `time` with `str(time)`
# OR subprocess.call([r"C:\Windows\system32\shutdown.exe", "/t", str(time)])
# specified the absolute path of the shutdown.exe
# The path may vary according to the installation.
或
import os
time = 10
os.system("shutdown /t %s " % time)
# `str` is not required, so removed.