有人能告诉我我在python crontab上做错了什么吗?
from crontab import CronTab
system_cron = CronTab()
生成以下错误:
File "test.py", line 3, in <module>
cron = CronTab()
TypeError: __init__() takes exactly 2 arguments (1 given)
当我直接从包指令中尝试其他变体的示例时,我也遇到了同样的问题:
my_user_cron = CronTab(user=True)
users_cron = CronTab(user='username')
我还尝试过用这种方法创建对象,我在python-crontab.py文件中找到了这种方法:
cron = CronTab(tab='')
但它会生成以下错误:TypeError: __init__() got an unexpected keyword argument 'tab'
我试着查看包中的代码,看看这是否是一个文档错误,并想办法解决问题,但这超出了我的技能水平。我相信这是定义我应该如何创建crontab对象的代码:
def __init__(self, user=None, tab=None, tabfile=None, log=None):
if user == True and not WINOS:
user = pwd.getpwuid( os.getuid() )[ 0 ]
self.lines = None
self.crons = None
self.filen = None
# Protect windows users
self.root = not WINOS and os.getuid() == 0
self.user = user
# Detect older unixes and help them out.
self.intab = tab
self.read(tabfile)
self._log = log
你觉得我做错了什么吗?
help(CronTab)返回:
class CronTab(__builtin__.object)
| Methods defined here:
|
| __init__(self, crontab)
|
| next(self, now=None, increments=[<function <lambda>>, <function <lambda>>, <function <lambda>>, <function _month_incr>, <function <lambda>>, <function _year_incr>, <function <lambda>>, <function <lambda>>, <function <lambda>>, <function <lambda>>, <function <lambda>>], delta=True)
| How long to wait in seconds before this crontab entry can next be
| executed.
|
| previous(self, now=None, delta=True)
|
| test(self, entry)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| matchers
您已经安装了crontab包,您显示的文档是针对python crontab的。它们是两个完全不同的包。
如果您在使用CronTab时得到错误TypeError:init()正好需要2个参数,则表明您安装了错误的模块。您需要从pypi或本地包管理器安装python-crontab,而不是crontab,然后重试。
参考:https://pypi.org/project/python-crontab/
卸载crontab
pip uninstall crontab
&安装python crontab
pip install python-crontab
cron = CronTab(user='name')
TypeError: __init__() got an unexpected keyword argument 'user'
如果您出现上述错误,请执行以下操作:
使用卸载crontab
pip uninstall crontab
并安装
pip install python-crontab