python - Opening and closing a file while logging data
问题描述
So, I'm logging temperature and humidity data from a DHT22 hooked up to the GPIO on a raspberry pi. It logs everything correctly - but I can only see the updated log after I stop logger.py running.
I think the problem is that I'm not closing the file after writing to it - but I'm not sure. Can I just add a f = open(xxx) and f.close() to the loop so that it 'saves' it everytime it logs?
import os
import time
import Adafruit_DHT
DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4
try:
f = open('/home/pi/temphumid/log.csv', 'a+')
if os.stat('/home/pi/temphumid/log.csv').st_size == 0:
f.write('Date,Time,Temperature,Humidity\r\n')
except:
pass
while True:
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
if humidity is not None and temperature is not None:
f.write('{0},{1},{2:0.1f}*C,{3:0.1f}%\r\n'.format(time.strftime('%m/%d/%y'), time.strftime('%H:%M:%S'), temperature, humidity))
else:
print("Failed to retrieve data from humidity sensor")
time.sleep(60)
expected: log.csv is updated, so that if I use tail log.csv I can see the up to date data.
actual: log.csv doesn't update until I stop logger.py from running (using sigint from htop as it is currently run as a cronjob on boot).
解决方案
Every time we open a file we need to close it to push the output to disk:
fp = open("./file.txt", "w")
fp.write("Hello, World")
fp.close()
To avoid calling the close() method every time, we can use the context manager of the open() function, which will automatically close the file after exiting the block:
with open("./file.txt", "w") as fp:
fp.write("Hello, World")
We do not need to call here the close method every time to push the data into the file.
推荐阅读
- sql-server - 如何将 SQL Server 表的所有者从域\用户名更改为 dbo?
- image - 加工中的自然色彩混合
- javascript - 如何在(as)peerDependencies 下安装包?
- c++ - boost read_graphml 丢弃节点的 id
- elasticsearch - ElasticSearch - 导出巨大的索引
- ios - AudioKit:尝试将音频从麦克风录制到文件,但没有录制任何内容
- ssrs-2016 - 如何在 SSRS 中创建将今天的日期传递给参数的报告订阅?
- c - 指向节点的双指针
- r - 如何从 R 中特定点的 IRT 模型中获取拟合曲线的值?
- hive - 具有求和分析功能的 Hive 查询