Форум сайта python.su
Подскажите, каким образом, запустив один раз на исполнение код, в котором есть обращение к файлу, и файл имеет название+data_stamp, каждый раз, да хоть раз в минуту или раз в час, файл создавался вновь и все писалось в него. Конкретный пример:
date_stmp = time.strftime("%Y-%m-%d-%H:%M") with open('out.txt_'+date_stmp, 'a') as f: f.write("\nReceived message '") f.write(data) f.write("' ;") # f.close() f.flush()
Отредактировано iandriyanov (Июль 17, 2012 10:43:06)
Офлайн
Весь код покажи
Офлайн
while True: data,addr = UDPSock.recvfrom(buf) if data: res = simplejson.loads(data) if res.get('EVENTID', None) in ["41", "42"]: try: with get_conn(pool) as con_sel: cur_sel = con_sel.cursor() cur_sel.execute("select sid from users where wiegand26=%s ", (res['CARDNUMBER'], )) sid = cur_sel.fetchone()[0] cur_sel.execute("select id from device where name=%s", (res['POINT'], )) dev_id = cur_sel.fetchone()[0] cur_sel.execute("insert into events(user_sid, device_sid, event_type_id, event_date, add_info) values (%s, %s, %s, %s, %s)", (sid, dev_id, res['EVENTID'], res['DATATIME'], res['DEP'] )) con_sel.commit() except Exception as e: with open('out.txt_'+date_stmp, 'a') as f: # print "-=-=-=-=\n",e,"\n=-=-=-=-" # import traceback # traceback.print_exc() f.write("\nReceived message '") f.write(data) f.write("' ;") f.close() # f.flush() print "\nReceived message '", data,"'" else: # print("error",res.get('EVENTID', None)) print "error",res.get('EVENTID', None)
Офлайн
Пять раз посмотрел и не нашел, где же определяется загадочная переменная date_stmp
Офлайн
Первый коммент же?
Офлайн
iandriyanovЗдесь где она определяется????while True: data,addr = UDPSock.recvfrom(buf) if data: res = simplejson.loads(data) if res.get('EVENTID', None) in ["41", "42"]: try: with get_conn(pool) as con_sel: cur_sel = con_sel.cursor() cur_sel.execute("select sid from users where wiegand26=%s ", (res['CARDNUMBER'], )) sid = cur_sel.fetchone()[0] cur_sel.execute("select id from device where name=%s", (res['POINT'], )) dev_id = cur_sel.fetchone()[0] cur_sel.execute("insert into events(user_sid, device_sid, event_type_id, event_date, add_info) values (%s, %s, %s, %s, %s)", (sid, dev_id, res['EVENTID'], res['DATATIME'], res['DEP'] )) con_sel.commit() except Exception as e: with open('out.txt_'+date_stmp, 'a') as f: # print "-=-=-=-=\n",e,"\n=-=-=-=-" # import traceback # traceback.print_exc() f.write("\nReceived message '") f.write(data) f.write("' ;") f.close() # f.flush() print "\nReceived message '", data,"'" else: # print("error",res.get('EVENTID', None)) print "error",res.get('EVENTID', None)
Офлайн
Какая разница где. первый комментарий, это кусок кода. И это кусок кода. Переменная определяется выще в коде.
Ну для наглядности пожалуйте:
import time date_stmp = time.strftime("%Y-%m-%d-%H:%M") .... while True: data,addr = UDPSock.recvfrom(buf) if data: res = simplejson.loads(data) if res.get('EVENTID', None) in ["41", "42"]: try: with get_conn(pool) as con_sel: cur_sel = con_sel.cursor() cur_sel.execute("select sid from users where wiegand26=%s ", (res['CARDNUMBER'], )) sid = cur_sel.fetchone()[0] cur_sel.execute("select id from device where name=%s", (res['POINT'], )) dev_id = cur_sel.fetchone()[0] cur_sel.execute("insert into events(user_sid, device_sid, event_type_id, event_date, add_info) values (%s, %s, %s, %s, %s)", (sid, dev_id, res['EVENTID'], res['DATATIME'], res['DEP'] )) con_sel.commit() except Exception as e: with open('out.txt_'+date_stmp, 'a') as f: # print "-=-=-=-=\n",e,"\n=-=-=-=-" # import traceback # traceback.print_exc() f.write("\nReceived message '") f.write(data) f.write("' ;") f.close() # f.flush() print "\nReceived message '", data,"'" else: # print("error",res.get('EVENTID', None)) print "error",res.get('EVENTID', None)
Офлайн
iandriyanovСрифмовать?
Какая разница.
Офлайн
По тому же суахили, я и пишу сюда! Где косяк?
Вернее где он это понятно. Как рефрешить переменную?
Отредактировано iandriyanov (Июль 17, 2012 09:21:27)
Офлайн
Я попробую еще раз.
# -*- coding:utf-8 -*- import time import threading date_stmp = time.strftime("%Y-%m-%d-%H:%M") def p(): print 'now=', time.strftime("%Y-%m-%d-%H:%M") print 'date_stmp=', date_stmp t = threading.Timer(70.0, p) t.start()
now= 2012-07-17-13:39 date_stmp= 2012-07-17-13:38
Офлайн