首页 > 解决方案 > 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).

标签: pythonfopenfclose

解决方案


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.


推荐阅读