Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File not appending
#1
Hi All,

I have been testing a small program for a little while now and recently noticed that the program was not logging any data to my text file. For some reason, if I start out with deleting the file, it will start logging. This has happened to me a couple of times, but I cannot see anything that looks wrong in my code for appending to the text file. If the file does not write, I do not get any error messages of any kind and everything else about it works as expected.

#!/usr/bin/env python3
 
import tkinter as tk
import tkinter.ttk as ttk
import RPi.GPIO as GPIO
from tkinter import *
import time
import shutil
from datetime import datetime
from tkinter import messagebox, filedialog
 
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN)
GPIO.setup(6, GPIO.IN)
 
 
class App(Frame):
 
    def __init__(self,master=None):
        Frame.__init__(self, master)
        root.attributes('-fullscreen', True)
        button1 = Button (master, text = "COPY ESD", command=lambda: self.esd_file())
        button1.place(x=275, y=285)
        button = Button (master, text = "EXIT ESD", command = quit)
        button.place(x=380, y=285)

 
        tk.Label(master, text="Badge #",font=("Helvetica", 30)).grid(row=0)
        tk.Entry(root).place(x=170,y=10, width=200, height=40)
        self.master = master
        self.label = Label(text="", fg="blue", font=("Helvetica", 30))         
        self.label.place(x=40,y=150)
        self.entry_widget = tk.Entry(root, font=("Helvetica", 26))
        self.entry_widget.focus_set()
        self.entry_widget.place(x=170,y=10, width=200, height=40)                   
        self.update_clock()
       
 
    def update_clock(self):
        d = datetime.now().strftime("%m-%d-%Y %H:%M:%S")
        newT = datetime.strptime(d, "%m-%d-%Y %H:%M:%S").strftime("%m-%d-%Y %I:%M:%S %p")
        now = time.strftime("%H:%M:%S")

        if not self.entry_widget.get():
            self.label.configure(text=newT + '\n' + '\n'"NOT READY / SCAN BADGE", fg="red", font=("Helvetica", 24))
            self.label.place(x=30,y=100)
            self.after(1000, self.update_clock)
        else:
            self.label.configure(text=newT + '\n' + '\n'"    TESTER READY! ", fg="blue", font=("Helvetica", 24))
            self.label.place(x=60,y=100)
            self.after(1000, self.update_clock)
            
         
 
        if GPIO.input(5) == 1 and len(self.entry_widget.get()) > 6:
            text_entered = self.entry_widget.get()
            self.label.configure(text="PASS",fg="green", font=('Helvetica 110 bold'))
            self.label.place(x=48,y=100)
            self.after(20000, self.update_text)
            FileName = str("/home/pi/esd.txt")
            with open(FileName, "a") as f: # open file
                f.write("Badge# " + text_entered + ", Test PASSED ON: " + newT + '\n')
            self.entry_widget.selection_range(0, END)   
      
        elif GPIO.input(6) == 1 and len(self.entry_widget.get()) > 6:
            text_entered = self.entry_widget.get()
            self.label.configure(text="FAIL",fg="red", font=('Helvetica 110 bold'))
            self.label.place(x=80,y=100)
            self.after(20000, self.update_text)
            FileName = str("/home/pi/esd.txt")
            with open(FileName, "a") as f: # open file
                f.write("Badge# " + text_entered + ", Test FAILED ON: " + newT + '\n')
            self.entry_widget.selection_range(0, END)
 
    def update_text(self):
        self.entry_widget.delete(0, 'end')

    def esd_file(self):
        try:
            shutil.copyfile(src="/home/pi/esd.txt", dst="/media/pi/ESD-USB/esd/esd.txt")
            messagebox.showinfo('information', 'Transfer Complete!')
        except IOError as e:
            messagebox.showerror('information', 'Transfer Failed!')
 
    def quit(self):
        root.destroy()
 
 
root = Tk()
app=App(root)
root.wm_title("ESD TESTER")
root.geometry("520x480")
root.after(1000, app.update_clock)
root.mainloop()
Reply
#2
Add the following:
after each 'with open' (lines 62 and 72)
                print(f"Badge# {text_entered}, Test FAILED ON: {newT}")
rerun and see if writes being called
Reply
#3
Thanks Larz60+ I will give that a try. The issue "seems" to be that once it doesn't write the first time, it just continues that way until I completely delete the file and let it start over. It was being used for about 2 weeks before I noticed today that the last entry was when I tested it about 3 weeks ago. All I did was delete the file and 'poof' it started working fine. So after 2 weeks, I should have probably 150-200 lines of data. It runs on a Raspberry Pi, which has also been rebooted a few times during the 2 weeks. This is so odd...
Reply
#4
rather than assuming all is well, try the print statements and have proof one way or other.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Appending a row of data in an MS Excel file azizrasul 3 1,139 Nov-06-2022, 05:17 PM
Last Post: azizrasul
  Print to a New Line when Appending File DaveG 0 1,190 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Appending Excel column value as CSV file name sh1704 0 1,269 Feb-06-2022, 10:32 PM
Last Post: sh1704
  Appending some rows in a .csv file from another .csv file tester_V 1 1,560 Aug-26-2020, 01:04 AM
Last Post: tester_V
  Appending a dictionary to csv file zahra_vaziri 0 3,249 Apr-14-2020, 04:30 AM
Last Post: zahra_vaziri
  Appending data into a file in tab delimited format metro17 1 4,072 Aug-06-2019, 07:34 AM
Last Post: fishhook
  appending to the same csv file dervast 3 2,434 Jul-16-2019, 05:43 PM
Last Post: jefsummers
  appending json file Buddy 5 9,987 Oct-26-2017, 04:37 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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