Python Forum
Editing a txt document using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Editing a txt document using python
#1
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?
Reply


Messages In This Thread
Editing a txt document using python - by duckredbeard - Sep-23-2020, 02:25 PM
RE: Editing a txt document using python - by buran - Sep-23-2020, 02:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help editing a PDF omar 4 1,101 Oct-22-2022, 08:52 PM
Last Post: Larz60+
  How to get first 5 images form the document using Python BeautifulSoup sarath_unrelax 0 1,664 Dec-19-2019, 07:13 AM
Last Post: sarath_unrelax
  How to extrac a data table from Pdf document using Python Amit0503 1 2,255 May-15-2019, 05:38 AM
Last Post: Larz60+
  [split] Python Guide / Document? kumatul 2 2,762 Jan-16-2019, 12:49 AM
Last Post: micseydel
  Python Guide / Document? Qui_Ten 1 2,850 Jan-13-2019, 07:50 AM
Last Post: Gribouillis
  Can python be used to search a word document for combinations of 6 digits? gkirt1053 2 2,816 Nov-15-2018, 06:22 PM
Last Post: gkirt1053
  Text editing with python UK_SPB 1 2,425 Nov-26-2017, 08:31 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020