python - Writing varible strings and time to a file -


i writing first program in python (first program ever matter). it's simple program detects input (on raspberry pi) door chime , counts number of times goes off , prints number of times on screen followed date , time event occurs.

so want refine program bit; first thought write data file later reviewing. have figured out how have program create , open file , write simple strings it, getting write strings variable (x) , the variable 'time.strftime' has me stumped...

here's code:

 # first program  # version 1.1  # goal write each motion event file   import time   import rpi.gpio gpio  gpio.setmode(gpio.bcm)  gpio.setup(24,gpio.in) # input = gpio.input(24)   #temp code don't have keep walking sensor called in line commented out above.  = int(raw_input("enter number"))  x = 0  while true:     #if (gpio.input(24)):      #again temp code, 'if a>0:'     if a>0:             x += 1             print "there have been %d motion events!" % (x)             print "the last 1 on: "             print time.strftime("%m/%d/%y %h:%m:%s")             print              # open file hold history data             #this stuck...             open('history.dat', 'a') file:                     file.write('motion event recorded at: %s \n') %time.strftime("%m")                     file.close()             #pause program prevent multiple counts on single person triggering chime - folks slow ;)             time.sleep(4) 

python print works in different fashion.

try this:

print("there have been"+ str(x) +"motion events!") 

and this:

file.write('motion event recorded at: '+time.strftime("%m")+'\n') 

try posting error you're receiving it's easier people answer.

also, first time code, pretty good.


Comments