附加到Python中创建的文件
我有一个文件,我正在向其中写入数据,但是我希望命名约定有时间,该文件是在创建的,然后在创建新文本文件之前流到同一文件一小时 我的问题是,我在从GPIO读取时使用while循环来创建文本文件,因此当代码运行时,每次通过循环时,它都会创建一个新的文本文件。如何创建文件,然后在预定的时间内写入现有文件附加到Python中创建的文件,python,Python,我有一个文件,我正在向其中写入数据,但是我希望命名约定有时间,该文件是在创建的,然后在创建新文本文件之前流到同一文件一小时 我的问题是,我在从GPIO读取时使用while循环来创建文本文件,因此当代码运行时,每次通过循环时,它都会创建一个新的文本文件。如何创建文件,然后在预定的时间内写入现有文件 import spidev import time import datetime import os spi = spidev.SpiDev() spi.open(0,0)
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
导入spidev
导入时间
导入日期时间
导入操作系统
spi=spidev.spidev()
spi.打开(0,0)#打开spi的端口0
计数=0
def时间戳(fname,fmt='%Y-%m-%d-%H-%m-%S{fname}'):
#此函数用于添加时间和日期
return datetime.datetime.now().strftime(fmt).format(fname=fname)
def报警检查(int):
#此函数用于检查SPI读入的值
#如果值为0,则创建报警
如果int==0:
返回(“系统报警!”)
其他:
返回(“系统正常”)
def写入文件(int):
#此函数将当前值写入文件,
#它被读入的时间
#如果系统处于im报警状态
以open(‘SPI_输入值’,‘a’)作为输出:
output.write(“Input=“+str(int)+'\t'+时间戳('ADC')+'\t\t'+str(报警检查(int))+'\n'))
def readadc(adcnum):
#此函数将打开SPI并读取它以查看当前值
#然后将其写入文本值
#使用write_to_file函数
如果adcnum>7或adcnum<0:
返回-1
r=spi.xfer2([1,8+adcnum类似于:
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)
您需要动态创建文件名。目前它是一个硬编码字符串。以下是一个示例:
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')
这将不断追加到表示当前小时的文件中。当下一个小时开始时,它将自动创建一个新文件,并开始向其追加数据。文件名是一个固定字符串,它如何继续创建新文件?正如@poke指出的,您应该根据当前时间确定文件名。代码I最初had被指定为第二个,因此每次它在while循环中迭代时,它都会在第二个循环结束时创建一个新文件,我在考虑文件名必须有多具体,每小时启动一次应该可以完成这项工作。