我在 Windows 上使用 Python 2.7,并且正在编写一个同时使用时间和日期时间模块的脚本。我以前做过,但是 python 似乎对加载两个模块很敏感,而且我以前使用的方法似乎不起作用。以下是我使用的不同语法以及我目前遇到的错误。
首先我试过:
from datetime import *
from time import *
...
checktime = datetime.today() - timedelta(days=int(2))
checktime = checktime.timetuple()
...
filetimesecs = os.path.getmtime(webgatelogdir + '/' + fn)
file = webgatelogdir + '/' + fn
filetime = localtime(filetimesecs)
...
else: time.sleep(60)
错误:
else: time.sleep(60) AttributeError: 'builtin_function_or_method' object has no attribute 'sleep'
然后我尝试了:
from datetime import *
from time import *
...
checktime = datetime.today() - timedelta(days=int(2))
checktime = checktime.timetuple()
...
filetimesecs = os.path.getmtime(webgatelogdir + '/' + fn)
file = webgatelogdir + '/' + fn
filetime = localtime(filetimesecs)
...
#else: time.sleep(60) # comment out time.sleep statement
我没有错误,但也没有睡眠延迟。
接下来我尝试了:
from datetime import *
import time
...
checktime = datetime.today() - timedelta(days=int(2))
checktime = checktime.timetuple()
...
filetimesecs = os.path.getmtime(webgatelogdir + '/' + fn)
file = webgatelogdir + '/' + fn
filetime = localtime(filetimesecs)
...
#else: time.sleep(60) # comment out time.sleep statement
错误:
filetime = localtime(filetimesecs) NameError: name 'localtime' is not defined
另一个修改,我试过这个:
import time
import datetime
...
checktime = datetime.today() - timedelta(days=int(2))
checktime = checktime.timetuple()
...
filetimesecs = os.path.getmtime(webgatelogdir + '/' + fn)
file = webgatelogdir + '/' + fn
filetime = localtime(filetimesecs)
...
#else: time.sleep(60) # comment out time.sleep statement
错误
checktime = datetime.today() - timedelta(days=int(2)) AttributeError: 'module' object has no attribute 'today'
最后,我尝试了这个:
import time
from datetime import *
...
checktime = datetime.today() - timedelta(days=int(2))
checktime = checktime.timetuple()
...
filetimesecs = os.path.getmtime(webgatelogdir + '/' + fn)
file = webgatelogdir + '/' + fn
filetime = localtime(filetimesecs)
...
#else: time.sleep(60) # comment out time.sleep statement
错误:
checktime = datetime.today() - timedelta(days=int(2))
AttributeError: 'module' object has no attribute 'today'
所以我不确定如何让这两个模块很好地发挥作用。或者我需要另一种方法来延迟脚本。
建议?或者指出我犯的错误?
谢谢。