在特定时间在python文件中启动代码的Python代码

问题描述 投票:-2回答:2

我想在特定时间执行文件,因为那时我将不在执行该文件。

[我在想是否有一种方法可以在另一个文件中编写一些代码,该代码将启动该代码并使其保持运行状态,以便它可以在特定时间启动该代码-也许使用os和time,或者命令行。

python time schedule
2个回答
1
投票

您可以使用Python的内置sched模块:

import sched, time

def action():
    print("hello world")

# Set up scheduler
s = sched.scheduler(time.localtime, time.sleep)
# Schedule when you want the action to occur
s.enterabs(time.strptime('Tue March 15 15:55:17 2020'), 0, action)
# Block until the action has been run
s.run()

0
投票

怎么样:

import time, subprocess, sys
time.sleep(3600)  # wait 1 hour
subprocess.call([sys.executable, 'another-script.py'])
© www.soinside.com 2019 - 2024. All rights reserved.