[英]python create daily folder
I would like to read from the serial of RPi and store the data in a daily folder as a 'csv.' 我想从RPi序列中读取数据,并将数据作为“ 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). 我可以创建一个文件,然后从csv文件中写入数据/从中读取数据,并且串行通信现在可以与腻子一起使用(在另一个项目中进行过尝试)。 In the future, the comm is going to be between pi and a various sensor. 将来,通信将在pi和各种传感器之间进行。 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 任何帮助,将不胜感激
使用datetime.datetime.now().strftime("%Y-%d-%m")
创建文件夹名称,使用os.path.exists(...)
检查文件夹是否存在,并使用os.mkdir(...)
创建新文件夹。
Thank you @furas. 谢谢@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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.