Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot seem to use import time and import datetime in same script in Python
    text
    copied!<p>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.</p> <p>First I tried:</p> <pre><code>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) </code></pre> <p>ERROR:</p> <p><code>else: time.sleep(60) AttributeError: 'builtin_function_or_method' object has no attribute 'sleep'</code></p> <p>Then I tried:</p> <pre><code>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 </code></pre> <p>and I got no errors, but no sleep delay either.</p> <p>Next I tried:</p> <pre><code>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 </code></pre> <p>ERROR:</p> <p><code>filetime = localtime(filetimesecs) NameError: name 'localtime' is not defined</code></p> <p>Another modification and I tried this:</p> <pre><code>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 </code></pre> <p>ERROR</p> <p><code>checktime = datetime.today() - timedelta(days=int(2)) AttributeError: 'module' object has no attribute 'today'</code></p> <p>Finally, I tried this:</p> <pre><code>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 </code></pre> <p>ERROR:</p> <p><code>checktime = datetime.today() - timedelta(days=int(2)) AttributeError: 'module' object has no attribute 'today'</code></p> <p>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.</p> <p>Suggestions? Or pointers to mistakes that I made?</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload