Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using a script that takes the arduino serial and writes it to an out.dat file. The scripts works fine when run after boot, however when I added the script to /etc/rc.local it runs the script, but does not record the information

#!/usr/bin/python
# get lines of text from serial port, save them to a file

from __future__ import print_function
import serial, io
import datetime

addr  = '/dev/ttyACM0'  # serial port to read data from
baud  = 9600            # baud rate for serial port
fname = 'gps-log'+datetime.datetime.now().strftime("%m-%d-%Y %H-%M-%S")+'.dat'   # log file to save data in
fmode = 'a'             # log file mode = append

with serial.Serial(addr,9600) as pt, open(fname,fmode) as outf:
    spb = io.TextIOWrapper(io.BufferedRWPair(pt,pt,1),
        encoding='ascii', errors='ignore', newline='\r',line_buffering=True)
    spb.readline()  # throw away first line; likely to start mid-sentence (incomplete)
    while (1):
        x = spb.readline()  # read one line of text from serial port
        print (x,end='')    # echo line of text on-screen
        outf.write(x)       # write line of text to file
        outf.flush()        # make sure it actually gets written out

Credit to https://www.raspberrypi.org/forums/viewtopic.php?f=44&t=64545

Note, it is supposed to create a new file named after the date and time. When I boot to command line, it prints the serial, so I know the program has executed. However, when I power down there is no .dat file to be found.

share|improve this question
up vote 2 down vote accepted

fname = 'gps-log'+datetime.datetime.now().strftime("%m-%d-%Y %H-%M-%S")+'.dat'

Appears to not involve any path. Where are you expecting to find these files?

I think an init service which does this will default to writing to /, so you could check there. However, you probably want to append an absolute path so you have a better place to store these and find them.

fname = '/var/log/gps/gps-log'+ ....
share|improve this answer
1  
As @goldilocks points out the file may have been created but not where you are expecting it to be. You can use the find command to check: find / -name '*.py' – Steve Robillard Apr 12 at 19:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.