Logfile for my App not "rewriting"

A

Adam

Hello,

I have a small app I am creating to crawl a directory and check that
if it is moved to another a location it's path will not break a
character limit. Usually the Windows path limit.

Now the script is working but every time I want to scan again I have
to restart for the log files to be written. I want to just be able to
change the parameter as I please and click scan without having to
restart the app for the log file to change.
(I am new to programming so the code could be untidy for want of a
better word.)

Here's the code:

###Code###

#file/path finder
#indentation value 4
import os,glob
import Tkinter as tk


# Class to create the Application and UI
class Theapp:


def __init__(self):
# Create GUI parts. Will allign later with grid
self.mainWindow = tk.Tk()
self.mainWindow.geometry("%dx%d%+d%+d" % (700, 400, 0, 0)) #
(width,height, x, y)
self.labAll = tk.Label(self.mainWindow, text="All Files/
Folders:")
self.labVi = tk.Label(self.mainWindow, text="Violating Files/
Folders:")

#Create a sub-frame containing textbox and scrollbar for All
files scanned display
self.allTxtf = tk.Frame(self.mainWindow, width=700,
height=164)
self.scrlr1 = tk.Scrollbar(self.allTxtf)
self.txtBoxall = tk.Text(self.allTxtf, wrap=tk.CHAR,
yscrollcommand=self.scrlr1.set, width=700, height=164)
self.scrlr1.config(command = self.txtBoxall.yview)

#Create another sub-frame containing textbox and scrollbar for
the Violating files display
self.viTxtf = tk.Frame(self.mainWindow, width=700, height=164)
self.scrlr2 = tk.Scrollbar(self.viTxtf)
self. txtBoxvi = tk.Text(self.viTxtf, wrap=tk.CHAR,
yscrollcommand=self.scrlr2.set, width=700, height=164)
self.scrlr2.config(command = self.txtBoxvi.yview)

#Create another sub-frame to contain the controls
self.ctrlFrame = tk.Frame(self.mainWindow, width=700,
height=72)
self.labDir = tk.Label(self.ctrlFrame, text="Dir:")
self.entDir = tk.Entry(self.ctrlFrame)
self.labTardir = tk.Label(self.ctrlFrame, text="Target Dir:")
self.entTardir = tk.Entry(self.ctrlFrame)
self.labChar = tk.Label(self.ctrlFrame, text="Char. Limit:")
self.entChar = tk.Entry(self.ctrlFrame)
self.labVi2 = tk.Label(self.ctrlFrame, text="Violations: ")
self.btFind = tk.Button(self.ctrlFrame, text="Scan", command =
self.fillboxes)
self.btExit = tk.Button(self.ctrlFrame, text="Exit", command =
self.quitEvent)

#Use tkinter's grid geometry manager to allign and display the
GUI
self.mainWindow.grid()

#Change weight to allow resize
self.mainWindow.grid_rowconfigure(1, weight=1)
self.mainWindow.grid_rowconfigure(3, weight=1)
self.mainWindow.grid_columnconfigure(0, weight=1)

self.allTxtf.grid_rowconfigure(0, weight=1)
self.allTxtf.grid_columnconfigure(0, weight=1)
self.allTxtf.grid_columnconfigure(1, weight=0)

self.viTxtf.grid_rowconfigure(0, weight=1)
self.viTxtf.grid_columnconfigure(0, weight=1)
self.viTxtf.grid_columnconfigure(1, weight=0)

self.ctrlFrame.grid_columnconfigure(1, weight=1)
self.ctrlFrame.grid_columnconfigure(3, weight=1)
self.ctrlFrame.grid_columnconfigure(5, weight=1)

#Frist allign the 3 main frames
self.labAll.grid(row=0, column=0)
self.allTxtf.grid(row=1, column=0)
self.labVi.grid(row=2, column=0)
self.viTxtf.grid(row=3, column=0)
self.ctrlFrame.grid(row=4, column=0)
#Now allign the content of allTxtf
self.txtBoxall.grid(row=0, column=0)
self.scrlr1.grid(row=0, column=1, sticky=tk.N + tk.S)
#Now allign the content for viTxtf
self.txtBoxvi.grid(row=0, column=0)
self.scrlr2.grid(row=0, column=1, sticky=tk.N + tk.S)
#Now allign the content for ctrlFrame
self.labDir.grid(row=0, column=0, sticky=tk.E)
self.entDir.grid(row=0, column=1)
self.labTardir.grid(row=0, column=2)
self.entTardir.grid(row=0, column=3)
self.labChar.grid(row=0, column=4, sticky=tk.E)
self.entChar.grid(row=0, column=5)
self.labVi2.grid(row=0, column=6)
self.btFind.grid(row=0, column=7)
self.btExit.grid(row=0, column=8)
self.entChar.insert(0, "254") #Insert default value

def findallfiles(self, base):
self.results = []
for root,dirs,files in os.walk(base):
os.chdir(root)
self.scan = glob.glob("*")
for r in self.scan:
if root[-1] == "\\":
self.results.append(root + r)
else:
self.results.append(root + "\\" + r)
return self.results


def fillboxes(self):
os.remove('viFiles.txt')
print "logfile deleted"
self.txtBoxall.delete(1.0, tk.END)
self.txtBoxvi.delete(1.0, tk.END)
self.logfile = file("viFiles.txt", "w")
print "logfiles created"
self.dispresults = self.findallfiles(self.entDir.get())
index = 0
lenghth = 0
dif = 0
#Debug text input to command prompt
print "Dir: " + self.entDir.get() + "\n" + "Tar: " +
self.entTardir.get() + "\n" + "Char: " + self.entChar.get() + "\n"
for i in self.dispresults:
self.txtBoxall.insert(tk.END, i + "\n")
length = len(i) + len(self.entTardir.get()) -
len(self.entDir.get())
if length > int(self.entChar.get()):
index += 1
txt = "Violations: " + str(index)
dif = length - int(self.entChar.get())
self.logfile.write(i + "\n" + "\n" + "Violating File
#: " + str(index) + "\n" + "Shorten by: " + str(dif) + "\n" + "\n" +
"---------------------------------" + "\n")
self.txtBoxvi.insert(tk.END, i + "\n")
self.labVi2.configure(text=txt)
self.logfile.close()

def quitEvent(self):
raise SystemExit


if __name__ == "__main__":
app = Theapp()
app.mainWindow.mainloop()

###End Code###

TIA
Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top