Python Forum
Editing a txt document using python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Editing a txt document using python (/thread-29860.html)



Editing a txt document using python - duckredbeard - Sep-23-2020

I have a program that monitors a DHT11 temperature sensor and logs the data to a txt document. The program loops every 3 minutes, so it gets a new line quite often. I'd like to have it purge out the old stuff, maybe delete the first line and add a new last line. Another option I would consider is to bulk delete several lines every hour, using a different program if necessary.

import Adafruit_DHT
import time
from gpiozero import LED
import requests
from datetime import datetime

DHT_SENSOR1 = Adafruit_DHT.DHT11
DHT_PIN1 = 14
LED1 = LED(21)  #Pulses as long as program is running
LED2 = LED(16)  #Amber on light

LED2.on()
with open("/home/pi/LittleFridgeLog.txt", "a") as file:
    results = "Monitor service restarted" + "\n"
    file.write(results)

#r = requests.post("https://bit.ly/EDITEDFORPRIVACY")  #Message that service has started
r = requests.post("https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?deviceId=group.android&text=LFTempMonitorStart&apikey=EDITEDFORPRIVACY")
while True:    
    humidity1, temperature1 = Adafruit_DHT.read_retry(DHT_SENSOR1, DHT_PIN1)  #Little fridge
    LittleFridgeF = (temperature1 * 9/5) + 32
    LittleFridgeF = round(LittleFridgeF,2)
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    
    # Log time and temp to text file
    with open("/home/pi/LittleFridgeLog.txt", "a") as file:
        results = current_time + " Little Fridge:" + str(LittleFridgeF) + "\n"
        file.write(results)
    
    if LittleFridgeF < 40:  #Less than this temp is good.  Blue light on.
        print(current_time)
        print("Little Fridge:", LittleFridgeF)
        print("")
        LED1.blink(.05,5)

    else:
        LED1.blink(.3,.3)
        print(current_time)
        print("Little Fridge overtemp:", LittleFridgeF)
        print("")
        alert = "overtemplittlefridge " + str(LittleFridgeF)
        base = "autoremotejoaomgcd.appspot.com/sendmessage?key=EDITEDFORPRIVACY&message="
        alerturl = "https://"+base+alert
        r = requests.post(alerturl)
    
        
    time.sleep(180)
time.sleep(1)
Can this be done?


RE: Editing a txt document using python - buran - Sep-23-2020

Instead of appending, read the file in memory, skip/remove the lines at the top of the file and then add new data. Overwrite the existing file.
This said using sqlite3 database may be more convenient to work with than csv/txt file


RE: Editing a txt document using python - duckredbeard - Sep-23-2020

Actually, I would really like a google sheet to be the log for this. From there, I can use Tasker to get data and/or edit the sheet. I've never used sqlite.

I have no idea HOW to do what you suggest!


RE: Editing a txt document using python - duckredbeard - Sep-24-2020

I figured it out. I will try to post the code tomorrow after I implement it in my projects.