Mark Thornton
Mark Thornton

Reputation: 43

Appending to Created File in Python

I have a file, which I am writing data to, however I would like the naming convention to have the time, the file was created at and then stream to the same file for one hour before creating a new text file.

My problem is that I am using a while loop to create the text file while reading from a GPIO and so when the code runs, it creates a new text file every time it moves through the loop. How can I create the file, then write to the existing file for a predetermined amount of time?

import spidev
import time
import datetime
import os


spi = spidev.SpiDev()
spi.open(0,0)                   #open port 0 of the SPI
count = 0

def timeStamp(fname, fmt ='%Y-%m-%d-%H-%M-%S_{fname}'):

        #this function adds the time and date

        return datetime.datetime.now().strftime(fmt).format(fname = fname)


def alarm_check(int):

        #this function checks the value read in by the SPI
        #creating an alarm if the value is 0

        if int == 0:
                return ("System Alarm!")
        else:
                return ("System OK")


def write_to_file(int):

        # this function will write the current value to a file,
        # the time it was read in
        # and if the system was im alarm


        with open (('SPI_Input_Values'), 'a') as output:
                output.write("Input = " + str(int) + '\t' + timeStamp('ADC') + '\t\t' + str(alarm_check(int)) + '\n')




def readadc(adcnum):


        #this function will open the SPI and read it to see the current value
        # this will then be written to a text value
        # using the write_to_file function

    if adcnum > 7 or adcnum < 0:
        return -1
    r = spi.xfer2([1,8 + adcnum << 4,0])

    adcout = ((r[1] & 3) << 8) + r[2]
    return adcout


while True:
    Inp1 = int(round(readadc(0)/10.24))  # defines Inp1 as an integer to be read in
    write_to_file(Inp1)
    count = count +1
    time.sleep(0.1)                      # puts the systemm to sleep for 0.1 seconds

Upvotes: 0

Views: 168

Answers (2)

Martin Konecny
Martin Konecny

Reputation: 59681

You need to dynamically create your filename. At the moment it was a hardcoded string. Here's an example:

fname = 'SPI_Input_Values'
fmt ='%Y-%m-%d-%H'
date_str = datetime.datetime.now().strftime(fmt)

file_name = date_str + "_" + fname

with open ((file_name, 'a') as output:
    output.write("Input = " + str(int) + '\t' + timeStamp('ADC') + '\t\t' + str(alarm_check(int)) + '\n')

This will constantly append to the file that represents the current hour. When the next hour begins, it will automatically create a new file and begin appending data to it instead.

Upvotes: 1

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14975

Something like:

def write_to_file(output, int):
    output.write("Input = " + str(int) + '\t' + timeStamp('ADC') + '\t\t' + str(alarm_check(int)) + '\n')



with open (('SPI_Input_Values'), 'a') as output:
    while True:
        Inp1 = int(round(readadc(0)/10.24))  # defines Inp1 as an integer to be read in
        write_to_file(output, Inp1)
        count = count +1
        time.sleep(0.1) 

Upvotes: 1

Related Questions