Im using a raspberry pi with raspbian, Debain Wheezy Jan 2014 and python3
I'm starting a python script from rc.local that captures a keyboard input and writes to a file, without logging in.
If the file that the script is writing to has not been created yet, the first keyboard input registers on the screen but isn't written to the file. All subsequent writes work fine.
My code works fine when I run it from the command line as user that's logged in, the first line is written to the new file as expected.
EDITED CODE FROM MIDNIGHTER
#!/usr/bin/env python3.2
import sys
from datetime import datetime
def main():
f = open('/home/pi/cards.csv','r')
sim = f.read()
sim = sim.split('\n')
simSet = set(sim)
while True:
try:
log = open('logs', 'a')
puk = input() # text input, i.e., always a string
included = "true" if puk in simSet else "false"
print(included, puk)
log.write("{included: %s, time: %s, number: %s}, \n" % (included, datetime.now(), puk))
log.close()
except ValueError:
log.close()
main()
And the rc.local
sudo python3 /home/pi/rf1
Im just learning this, please excuse the poor execution.
SOLUTION
I realise now I left out an important detail about a cron job closing and copying the file that was being written to.
I found my answer here what exactly the python's file.flush() is doing?
Instead of file.close.()
I used file.flush()
and it works.
Code below:
#!/usr/bin/env python3.2
import sys
from datetime import datetime
def main():
f = open('/home/pi/cards.csv','r')
sim = f.read()
sim = sim.split('\n')
simSet = set(sim)
log = open('logs', 'a')
while True:
try:
puk = input() # text input, i.e., always a string
included = "true" if puk in simSet else "false"
print(included, puk)
log.write("{included: %s, time: %s, number: %s}, \n" % (included, datetime.now(), puk))
log.flush()
except ValueError:
log.flush()
main()