1

I would like to read from the serial of RPi and store the data in a daily folder as a 'csv.' file. I can create a file, write/read to/from csv file and had the serial comm working with putty for now (tried in a different project). In the future, the comm is going to be between pi and a various sensor. Considering everything else is working I am not sure how to create a seperate file automatically for each day. This is what I've done so far;

import serial
import time
import csv

def readLine(port)
    rv = ""
    while True:
        ch = port.read()
        rv += ch
        if ch == '\r' or ch =='':
            return rv

port = serial.Serial("/dev/ttyAMA0", baudrate = 115200, timeout = 10)

while True:
    rcv=readLineCR(port)

str1 = time.strftime("%d%m%y")
file = open('directory....')

with open('test.csv', 'w') as fp:
     a = csv.writer(fp, delimiter=',')
     # data to be tested
     data = [[str1,'1234'],[str1,'4321']]
     a.writerows(data)
     print('csv is created on: ' + str1)

reader = csv.reader(file)
for line in reader:
print(line)

Any help would be appreciated

3
  • Show what you have tried! Commented May 5, 2014 at 13:11
  • Using a python logging.handlers.TimedRotatingFileHandler you can easily create a logging system where files rotate automatically. If you skip the foldering. Commented May 5, 2014 at 13:28
  • Thanks for the comment @msvalkon. Creating a logging system seems a little bit advanced at the moment. But will try in the future. Commented May 5, 2014 at 14:01

2 Answers 2

2

Use datetime.datetime.now().strftime("%Y-%d-%m") to create folder name, os.path.exists(...) to check if folder exists and os.mkdir(...) to create new folder.

Sign up to request clarification or add additional context in comments.

Comments

1

Thank you @furas. this is what I did and seems like its working.

import os
todayDate = time.strftime("%d-%m-%y")
directory = '/home/pi/...' + todayDate
if not os.path.exists(directory)
   os.makedirs(directory)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.