0

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 
3
  • The file name is a fixed string, how can it keep creating new files? Commented Jun 23, 2014 at 11:56
  • As @poke points out, you should determine your file name based on the current time. Commented Jun 23, 2014 at 11:57
  • The code I originally had was specified to the second, so each time it iterated through the while loop it created a new file as a second had passed, i was over thinking how specific the file name had to be, having one start every hour should do the job. Commented Jun 23, 2014 at 19:28

2 Answers 2

1

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.

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

Comments

1

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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.