-2

我有这个在提示符下运行的命令:

echo "python setHeater.py" | at 16:30

我如何从 Python 程序中执行它?

在我的程序中,我创建了一个日期,并将其连接到一个字符串,就像这样

newtime = createnewtime()
commandToExecute = 'echo "python setHeater.py" | at ' + newtime 
#and then here the code to actually run the command in the command environment
4

2 回答 2

1

subprocess基本上,您可以使用以下库执行命令:

from subprocess import Popen, PIPE

newtime = createnewtime()
p1 = Popen(["echo ", "'python setHeater.py'"], stdout=PIPE)
p2 = Popen(["at", newtime ], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
于 2013-03-25T15:38:55.290 回答
1

您可以使用操作系统库:

import os

newtime = createnewtime()
command = 'echo "python setHeater.py" | at ' + newtime
os.system(command)

尽管如果您尝试执行此命令,则不需要使用“echo”。简单地:

import os

newtime = createnewtime()
command = "python setHeater.py | at " + newtime
os.system(command)
于 2013-03-25T15:53:01.520 回答