分时间段执行多个python文件
时间: 2023-11-16 08:04:13 浏览: 36
有几种方法可以分时间段执行多个Python文件,以下是其中的两种方法:
方法一:使用crontab
1. 打开终端并输入以下命令打开crontab编辑器:
```
crontab -e
```
2. 在编辑器中添加以下行:
```
0 9 * * * python /path/to/file1.py
30 9 * * * python /path/to/file2.py
0 10 * * * python /path/to/file3.py
```
这里的数字表示分钟和小时,表示每天的9:00执行file1.py,9:30执行file2.py,10:00执行file3.py。
3. 保存并退出。
方法二:使用Python的时间模块
1. 在每个Python脚本的开头添加以下代码:
```python
import time
while True:
if time.localtime().tm_hour == 9 and time.localtime().tm_min == 0:
# 执行file1.py的代码
elif time.localtime().tm_hour == 9 and time.localtime().tm_min == 30:
# 执行file2.py的代码
elif time.localtime().tm_hour == 10 and time.localtime().tm_min == 0:
# 执行file3.py的代码
time.sleep(60)
```
这里的数字表示小时和分钟,表示每天的9:00执行file1.py,9:30执行file2.py,10:00执行file3.py。
2. 保存并运行每个Python脚本。