I'm using Python 2.7 on Windows and I am writing a script that uses both time and datetime modules. I've done this before, but python seems to be touchy about having both modules loaded and the methods I've used before don't seem to be working. Here are the different syntax I've used and the errors I am currently getting.
First I tried:
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)
ERROR:
else: time.sleep(60) AttributeError: 'builtin_function_or_method' object has no attribute 'sleep'
Then I tried:
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
and I got no errors, but no sleep delay either.
Next I tried:
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
ERROR:
filetime = localtime(filetimesecs) NameError: name 'localtime' is not defined
Another modification and I tried this:
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
ERROR
checktime = datetime.today() - timedelta(days=int(2)) AttributeError: 'module' object has no attribute 'today'
Finally, I tried this:
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
ERROR:
checktime = datetime.today() - timedelta(days=int(2))
AttributeError: 'module' object has no attribute 'today'
So I'm not sure how to get the two modules to play nicely. Or I need another method to put a delay in the script.
Suggestions? Or pointers to mistakes that I made?
Thanks.
datetime is a built-in Python module. Developers don't need to install it separately. It's right there. With the datetime module, you get to work with different classes that work perfectly with date and time.
datetime refers to the datetime class within the datetime module. If you do 'from datetime import datetime', you're only importing the datetime class, so when you refer to 'datetime' in your code, it's referring to the datetime class, not the whole datetime module.
Python datetime: datetime. now(tz=None) returns the current local date and time. If optional argument tz is None or not specified, this is like today().
You can use as while importing time.
import time as t
from datetime import datetime
...
t.sleep(2)
Don't use from ... import *
– this is a convenience syntax for interactive use, and leads to confusion in scripts.
Here' a version that should work:
import time
import datetime
...
checktime = datetime.datetime.today() - datetime.timedelta(days=int(2))
checktime = checktime.timetuple()
...
filetimesecs = os.path.getmtime(webgatelogdir + '/' + fn)
file = webgatelogdir + '/' + fn
filetime = time.localtime(filetimesecs)
...
#else: time.sleep(60) # comment out time.sleep statement
When importing the modules using import <modulename>
, you of course need to use fully qualified names for all names in these modules
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With