AttributeError: 'list' object has no attribute 'split' - Print next crontab scheduled job with croniter - stdout - Python

AttributeError: 'list' object has no attribute 'split' - Print next crontab scheduled job with croniter - stdout - Python

我需要阅读 /etc/crontab 文件的内容。

现在我有这个:

import croniter
import datetime

now = datetime.datetime.now()


def main():

    f = open("/etc/crontab","r")
    f1 = f.readlines()
    cron = croniter.croniter(f1, now)
    for x in f1:
        cron.get_next(datetime.datetime)
        print(x)

if __name__ == "__main__":
    main()

我想要的是在下一次任务 运行 时打印,根据我的 crontab 文件中定义的内容,我已遵循 this answer,但是我需要实际从文件(即 crotab 文件)然后将其打印到标准输出。

现在它抛给我这个:

Traceback (most recent call last):
File "cron.py", line 17, in <module>
main()
File "cron.py", line 11, in main
cron = croniter.croniter(f1, now)
File "/home/user/.virtualenvs/rest_tails2/lib/python3.6/site-packages/croniter/croniter.py", line 92, in __init__
self.expanded, self.nth_weekday_of_month = self.expand(expr_format)
File "/home/user/.virtualenvs/rest_tails2/lib/python3.6/site-packages/croniter/croniter.py", line 464, in expand
expressions = expr_format.split()
AttributeError: 'list' object has no attribute 'split'

对此有什么想法吗?我是 croniter 的新手,还有 python-crontab 但还没用过。

croniter 处理单个 cron 表达式。您应该将它放在循环中,并将其分别应用于每一行:

for x in f1:
    cron = croniter.croniter(x, now) # Here!
    cron.get_next(datetime.datetime)
    print(x)