Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将来运行python脚本_Python_Datetime_Time - Fatal编程技术网

将来运行python脚本

将来运行python脚本,python,datetime,time,Python,Datetime,Time,我有一个python脚本,需要在用户输入上运行。我将从用户那里获得日期和时间,脚本将在该日期运行 import time from datetime import datetime today = datetime.today() enter_date = raw_input("Please enter a date:(2017-11-28): ") enter_time = raw_input("What time do you want to start the script? ")

我有一个python脚本,需要在用户输入上运行。我将从用户那里获得日期和时间,脚本将在该日期运行

import time
from datetime import datetime

today = datetime.today() 
enter_date = raw_input("Please enter a date:(2017-11-28): ")
enter_time = raw_input("What time do you want to start the script? ")

difference = enter_date - today

time.sleep(difference)
在此之后,main()函数将运行

我跑不动了。有没有更好的方法来完成这项任务?谢谢

import time
from datetime import datetime

date_format = "%d/%m/%y %H:%M"
today = datetime.today() 

print("Please enter date in format dd/mm/yy HH:MM")

enter_date = datetime.strptime(
    raw_input("> "),
    date_format)

difference = enter_date - today

print(difference)


#time.sleep(difference.total_seconds())
如果你愿意,取消睡眠注释。然而,这是一个糟糕的解决方案。您应该使用特定于操作系统的功能来实现这一点。例如,您可以在linux上向cronjob文件添加带有脚本的行,等等。

在这里

import time
from datetime import datetime

today = datetime.today() 
input_date = raw_input("Please enter a date and time : 2017-12-31 18:02 : ")

enter_date=datetime.strptime(input_date,'%Y-%m-%d %H:%M')

difference = enter_date - today

print difference.total_seconds()

time.sleep(difference.total_seconds())
如果您想将日期和时间分开,请使用

import time
import datetime

today = datetime.datetime.today() 
input_date = raw_input("Please enter a date 2017-12-31  : ")
input_time=raw_input("What time do you want to start the script? 18:02 : ")

enter_date=datetime.datetime.strptime(input_date,'%Y-%m-%d')
enter_time=input_time.split(':')
date_with_time = enter_date + datetime.timedelta(hours=int(enter_time[0]),minutes=int(enter_time[1]))

difference = date_with_time - today

print difference.total_seconds()

time.sleep(difference.total_seconds())

但是首选的方法是使用cron作业

如果您可以执行cron作业,一个很好的解决方案是将脚本分为两部分,一部分是请求何时执行脚本,另一部分是实际脚本

您的第一个脚本将创建一个cron作业来计划脚本的执行,为此,您可以使用python crontab包:

pip install python-crontab
要创建cron作业,请执行以下操作:

from datetime import datetime
from crontab import CronTab

run_date_input = raw_input("Please enter a date (e.g. 2017-11-28): ")
run_time_input = raw_input("What time do you want to start the script (e.g. 14:30:12)? ")

run_date = datetime.strptime(run_date_input, "%y-%m-%d")
run_time = datetime.strptime(run_time_input, "%H:%M:%S")

#Access the crontab for the current user (on unix systems)
user_cron = CronTab(user='your username')

#Access the crontab (on windows sytems)
user_cron = CronTab(tabfile='c:/path_to_your_tab_file/filename.tab')

#Create a new job
job = user_cron.new(command='python /home/your_second_script.py')
job.setall(datetime(run_date.year, run_date.month, run_date.day, run_time.hour, run_time.minute, run_time.second))

如果您使用windows,则需要使用类似windows或windows 10的新Linux子系统来安装cron for windows。

在操作系统中创建一个任务以运行脚本
enter_date
是一个字符串,您需要在计算差异之前将其转换为日期对象。您是使用windows还是Linux(unix)?如果您正在学习Python,您最好以Python3为目标。旧版本已经被定在明年到期,不过由于几年前提货缓慢,最后期限延长了几年。如果您不维护遗留脚本,就没有理由继续使用Python 2。您可以使用标准库中的
sched
Python模块:非常感谢@ismay您可以在windows上作为服务运行Python crontab。(更多的测试会更好)看到了吗