将可变时间设置为SchTasks

时间:2013-11-13 05:55:03

标签: python python-2.7 subprocess python-datetime schtasks

这是我到目前为止所做的事情

import subprocess
import datetime

now = datetime.datetime.now()
now_plus_10 = now + datetime.timedelta(seconds = 10)
path=r"path_to_file"
subprocess.call(['SchTasks', '/Create','/SC', 'ONCE','/TN', 'test1','/TR', path,'/ST', now_plus_10])
subprocess.call(['SchTasks', '/Create','/SC', 'ONCE','/TN', 'test2','/TR', path,'/ST', now_plus_10])

我不断得到的错误是:

needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: argument of type 'datetime.datetime' is not iterable

如果我像这样直接使用时间,则任务安排成功:

time="09:06"
subprocess.call(['SchTasks', '/Create','/SC', 'ONCE','/TN', 'test3','/TR', path,'/ST', time])

如果还有其他方法可以做到这一点,我们将不胜感激。

提前致谢!

2 个答案:

答案 0 :(得分:0)

needquote = (" " in arg) or ("\t" in arg) or not arg

我认为arg的类型为datetime.datetime,您正在使用in运算符。 in运算符仅适用于可以迭代的对象,例如列表,字符串,集等。因此,您需要做的是,使用argstrftime转换为字符串

答案 1 :(得分:0)

很抱歉打扰你们。我现在得到了答案

import subprocess
import time

currenttime = time.time()
new= time.strftime("%H:%M:%S", time.localtime(currenttime + 0.6* 60))
path=r"path_to_file"
subprocess.call(['SchTasks', '/Create','/SC', 'ONCE','/TN', 'test1','/TR', path,'/ST', new])
subprocess.call(['SchTasks', '/Create','/SC', 'ONCE','/TN', 'test2','/TR', path,'/ST', new])

感谢您的帮助!

相关问题