I have imported the following modules at the top of my python script:
import os
import sys
import time
import datetime
import random
import pprint
from random import randint
from time import sleep
from datetime import datetime, timedelta
But, I am still getting a module object error for this part of my code:
def check(low,high):
with open('done.txt', 'r+') as done:
ts = time.time()
sttime = datetime.fromtimestamp(ts).strftime('%Y%m%d_%H:%M:%S - ')
done_completed = open('done_completed.txt', "a")
for line in done:
now = datetime.now()
now_time = now.time()
if now_time >= time(23,30) and now_time <= time(06,30):
print "sleeping"
sleep(5000)
else:
done_id = line.strip()[20:]
then = datetime.strptime(line.strip()[:17], '%Y%m%d_%H:%M:%S')
(There's another if elif elif under all that but I figured it isn't relevant to the error)
(Yes, I am a python beginner)
The error is:
File "/home/joe/Desktop/follomatic/follomatic.py", line 85, in check
if now_time >= time(23,30) and now_time <= time(06,30):
TypeError: 'module' object is not callable
Does anyone see what is wrong? Have I not specified to call the module in the right way?
Thanks :)
Python's datetime module is a bit of a rat's nest. :) And it can get even more confusing if you use from imports.
This code should clarify things a bit. Note that this is not a great way to do things. It's generally better to work with datetime.datetime objects rather than datetime.time objects, otherwise things get messy if you need to handle time intervals that include midnight (or multiple days). But I wrote this code to roughly correspond with the code in your question, and I wanted to make it easy to test at any time of day.
#!/usr/bin/env python
''' datetime.time manipulation demo
From http://stackoverflow.com/q/28605732/4014959
Written by PM 2Ring 2015.02.19
'''
from time import sleep
import datetime
now = datetime.datetime.now()
now_time = now.time()
#A datetime.time example
t1 = datetime.time(5, 30)
print 't1 is', t1
now = datetime.datetime.now()
now_time = now.time()
print 'now is %s, now_time is %s' % (now, now_time)
begin_datetime = now + datetime.timedelta(seconds=5)
end_datetime = now + datetime.timedelta(seconds=10)
begin_time = begin_datetime.time()
end_time = end_datetime.time()
print 'begin', begin_time
print 'end', end_time
for i in xrange(15):
now_time = datetime.datetime.now().time()
print now_time, begin_time <= now_time <= end_time
sleep(1)
typical output
t1 is 05:30:00
now is 2015-02-19 23:44:39.152786, now_time is 23:44:39.152786
begin 23:44:44.152786
end 23:44:49.152786
23:44:39.152905 False
23:44:40.153999 False
23:44:41.155191 False
23:44:42.156398 False
23:44:43.156614 False
23:44:44.157810 True
23:44:45.159028 True
23:44:46.160231 True
23:44:47.161444 True
23:44:48.162660 True
23:44:49.163869 False
23:44:50.165076 False
23:44:51.166650 False
23:44:52.167842 False
23:44:53.169053 False
Related
i'm having this error with my code. Basically what i'm trying to do is to play a song a specific time. It's working when the hour between a range of hours however when i specify the hour and minute it's giving me an indentation error:
THIS ONE IS WORKING:
import time
import datetime
import os, random
import webbrowser
now = datetime.datetime.now()
now.hour
basedirC = "C:\\Users\Random"
file = random.choice([x for x in os.listdir(basedirC) if os.path.isfile(os.path.join(basedirC, x))])
if 18 < now.hour < 24 :
print ("It's evening")
webbrowser.open(os.path.join(basedirC, file))
else:
print (" It's not evening")
THIS IS WHERE I'M GETTING IT WRONG:
import time
import datetime
import os, random
import webbrowser
now = datetime.datetime.now()
now.hour
now.minute
basedirC = "C:\\Users\Random"
file = random.choice([x for x in os.listdir(basedirC) if os.path.isfile(os.path.join(basedirC, x))])
if now.hour = 22 and now.minute = 12 :
print ("It's evening")
webbrowser.open(os.path.join(basedirC, file))
else:
print (" It's not evening")
I'm getting: indentation errors on the print function and the webbrowser.open funtion. any clue?
= is an assignment staement which can't be used in a larger expression. Lately, the := augmented assignment statement has been added so that you can write things like that.
However, what you really wanted was the == equality comparison operator.
import time
import datetime
import os, random
import webbrowser
now = datetime.datetime.now()
# has no effect...
#now.hour
#now.minute
basedirC = "C:\\Users\Random"
file = random.choice([x for x in os.listdir(basedirC) if os.path.isfile(os.path.join(basedirC, x))])
if now.hour == 22 and now.minute == 12 :
print ("It's evening")
webbrowser.open(os.path.join(basedirC, file))
else:
print (" It's not evening")
But that's probably not quite what you want either... this if only runs for 1 minute a day at 22:12. If its 22:13, it won't think its evening. If you want your code to run at any time after 22:12, then you could take the time of day component from the datetime object and compare it to a given time:
if now.time() >= dt.time(22,21):
...
I wrote a program that will close the hard drive with BitLocker in Windows by using libraries datetime and subprocess but I cannot get the correct result.
Here is my code:
import subprocess
import datetime
now = datetime.datetime.today()
delta = datetime.timedelta(hours=2)
time_delta = now + delta
# print(time_delta)
# print(now)
try:
if now == time_delta:
close = subprocess.check_output(['manage-bde', '-lock', 'D:', '-ForceDismount'])
close.decode('utf-8').split('\n')
print('done')
except Exception:
pass
That is because you are checking the current time now = datetime.datetime.today(), then add 2 hours to it and immediately after check, if time_delta is equal to now.
Thus the if statement always results in false and the code in it does not get executed.
You need to put something in place to wait. Something like that.
import subprocess
import datetime
import time
now = datetime.datetime.today()
delta = datetime.timedelta(hours=2)
time_delta = now + delta
while now != time_delta:
time.sleep(1) # wait for a second
now = datetime.datetime.today()
# when now == time_delta we continue here:
try:
close = subprocess.check_output(['manage-bde', '-lock', 'D:', '-ForceDismount'])
close.decode('utf-8').split('\n')
print('done')
except Exception:
pass
This is not the best way to schedule a task, but it should be working.
I have a function to do some work. The code should repeat based on schedule. I used Croniter module. the schedule should be in cron type syntax. It works with every minutes. But it doesn't work with seconds. I added seconds at last in schedule but it says croniter syntax is not acceptable. How do I add seconds in syntax to make it work for seconds also?
Here is my code.
import yaml
from croniter import croniter
from datetime import datetime, timedelta
import time
def fun():
for i in items:
print (i)
def roundDownTime(dt=None, dateDelta=timedelta(minutes=1)):
roundTo = dateDelta.total_seconds()
if dt == None : dt = datetime.now()
seconds = (dt - dt.min).seconds
rounding = (seconds+roundTo/2) // roundTo * roundTo
return dt + timedelta(0,rounding-seconds,-dt.microsecond)
def getNextCronRunTime(schedule):
return croniter(schedule, datetime.now()).get_next(datetime)
def sleepTillTopOfNextMinute():
t = datetime.utcnow()
sleeptime = 60 - (t.second + t.microsecond/1000000.0)
time.sleep(sleeptime)
items =[2,4,6,7]
schedule = "*/1 * 7 2 * "
nextRunTime = getNextCronRunTime(schedule)
while True:
roundedDownTime = roundDownTime()
if (roundedDownTime == nextRunTime):
fun()
nextRunTime = getNextCronRunTime(schedule)
elif (roundedDownTime > nextRunTime):
# We missed an execution. Error. Re initialize.
nextRunTime = getNextCronRunTime(schedule)
sleepTillTopOfNextMinute()
I am looking for a comparison of two times in Python. One time is the real time from computer and the other time is stored in a string formatted like "01:23:00".
import time
ctime = time.strptime("%H:%M:%S") # this always takes system time
time2 = "08:00:00"
if (ctime > time2):
print("foo")
import datetime
now = datetime.datetime.now()
my_time_string = "01:20:33"
my_datetime = datetime.datetime.strptime(my_time_string, "%H:%M:%S")
# I am supposing that the date must be the same as now
my_datetime = now.replace(hour=my_datetime.time().hour, minute=my_datetime.time().minute, second=my_datetime.time().second, microsecond=0)
if (now > my_datetime):
print("Hello")
EDIT:
The above solution was not taking into account leap second days (23:59:60). Below is an updated version that deals with such cases:
import datetime
import calendar
import time
now = datetime.datetime.now()
my_time_string = "23:59:60" # leap second
my_time_string = now.strftime("%Y-%m-%d") + " " + my_time_string # I am supposing the date must be the same as now
my_time = time.strptime(my_time_string, "%Y-%m-%d %H:%M:%S")
my_datetime = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=calendar.timegm(my_time))
if (now > my_datetime):
print("Foo")
https://docs.python.org/2/library/datetime.html
The datetime module will parse dates, times, or combined date-time values into objects that can be compared.
from datetime import datetime
current_time = datetime.strftime(datetime.utcnow(),"%H:%M:%S") #output: 11:12:12
mytime = "10:12:34"
if current_time > mytime:
print "Time has passed."
I am having trouble with the following scenario using python schedule module. Essentially I want to run a login event at time A, and then run the action at time B.
The code does not run as the intended behaviour describes and this is where I need help.
import sched
import datetime
today = datetime.datetime.today()
log = today.replace(hour=11, minute=59, second = 0)
action= today.replace(hour=12, minute=0, second = 0)
scheduler = sched.scheduler(datetime.datetime.today(), time.sleep)
def login_event(name):
print 'EVENT:', datetime.datetime.today(), name
def action_event(name):
print 'EVENT:' datetime.datetime.today(),name
print 'START:', time.time()
scheduler.enter(log, login_event, ('Login'))
scheduler.enter(action, login_event, ('Action'))
scheduler.run()
EDIT I have altered the code to the following but it still doesn't seem right in terms of how best to implement this behaviour.
import sched
import datetime
from datetime import timedelta
import datetime
import time
today = datetime.datetime.today()
log = datetime.datetime.now() + timedelta(minutes=1)# today.replace(hour=12, minute=46, second = 0)
action= log + timedelta(minutes=2)
scheduler = sched.scheduler(time.time, time.sleep)
print datetime.datetime.now
def login_event(name):
print 'Login:', datetime.datetime.now(), name
def action_event(name):
print 'Action:', datetime.datetime.now(), name
print 'Start:', datetime.datetime.now()
scheduler.enter(1, 1, login_event, ('first',))
scheduler.enter(60, 1, action_event, ('second',))
scheduler.run()
The following code hasn't been tested but should be work.
I've put your original code into comment so you can see where you got wrong.
You will probably need to refer the doc: https://docs.python.org/2/library/sched.html
import sched, time
import datetime
today = datetime.datetime.today()
log = today.replace(hour=11, minute=59, second = 0)
action= today.replace(hour=12, minute=0, second = 0)
#scheduler = sched.scheduler(datetime.datetime.today(), time.sleep)
#The first argument of sched.scheduler should be a function that return a number.
scheduler = sched.scheduler(time.time, time.sleep)
def login_event(name):
print 'EVENT:', datetime.datetime.today(), name
def action_event(name):
print 'EVENT:', datetime.datetime.today(),name
print 'START:', time.time()
scheduler.enter is used for relative delay. The correct function to use is scheduler.enterabs
You will need a function to convert datetime to POSIX timestamp.
This can be tricky in python 2.x due to timezone issue.
Refer to this question: Convert datetime to Unix timestamp and convert it back in python
Also, the function takes 4 arguments.
#scheduler.enter(log, login_event, ('Login'))
#scheduler.enter(action, login_event, ('Action'))
scheduler.enterabs(timestamp(log), 0, login_event, ('Login'))
scheduler.enterabs(timestamp(action), 0, action_event, ('Action'))
scheduler.run()
https://github.com/dbader/schedule
By following the pattern linked above I was able to create the desired behaviour using a slightly different schedule module
import schedule
import time
def job():
print("I'm working on job one...")
def job2():
print("I'm working on job two..")
schedule.every().day.at("10:30").do(job)
schedule.every().day.at("10:35").do(job2)
while True:
schedule.run_pending()
time.sleep(1)