Alrighty, so I have this python script that copies text from multiple files and pastes it to new files of a different file extension. It's almost done however I have a problem with its refresh button.
To use it, just put some files in the 'Input' directory, run the program and go.
In the event that the user runs the program with no files in the 'Input' folder, sees that the file count is zero and then puts some files in the folder, I want the file_count variable to be reloaded.
At the moment, I have a function called 'refresh()' that opens another instance of the program and then closes the initial instance. This works, but because 'os.startfile' is not designed for this use, it has a few problems:
Firstly ofcourse, it seems like overkill to relaunch the entire program.
Secondly, 'os.startfile' only works on windows.
Thirdly, if the window is moved and then the program is 'refreshed', the new instance will open in the default position.
So is there anyway to re run just the 'file_count' variable?
PS. I'm self taught so there are gaps in my knowledge (apparently, this is one of them.) PPS. Just about to start a lecture, so I will only be able to reply in an hour.
import sys
import os, os.path
from os import listdir
import shutil
from Tkinter import *
from Tkinter import Tk
from tkFileDialog import askopenfilename
path, dirs, files = os.walk(os.path.join(os.getcwd(), 'Input')).next()
file_count = len(files)
dirList = os.listdir(path)
def refresh():
opencp = os.startfile(os.path.join(os.getcwd(), 'Copy_Paster.py'))
closecp = os.execl(sys.executable, *([sys.executable]+sys.argv))
def copy_paste():
# Copy pastes text from source documents to new ones.
for fname in dirList:
print fname
extension = text_entered.get()
copy = open(os.path.join(os.getcwd(), 'Input', fname), "r")
paste = open(os.path.join(os.getcwd(), 'Output', fname + extension), "w+")
copy.seek(0)
shutil.copyfileobj(copy,paste)
copy.close()
paste.close()
# Window Geometry
width = "250"
height = "70"
# Window
root = Tk()
root.geometry(width+"x"+height+"+500+200",)
root.resizable(0,0)
root.title("Copy Paster")
text_entered = StringVar()
counter = Label(text="Input Directory: " + str(file_count) + " files.")
counter.place(x=int(width)-248,y=int(height)-63)
refresh = Button(root,command=refresh,text="Refresh")#Why this no work?
refresh.place(x=int(width)-80,y=int(height)-65, width=70)
format_label = Label(text="Output File Format:")
format_label.place(x=int(width)-248,y=int(height)-35)
document_type = Entry(root, textvariable=text_entered)
document_type.place(x=int(width)-135,y=int(height)-33, width=40)
convert = Button(root,command=copy_paste,text="GO!")
convert.place(x=int(width)-80,y=int(height)-35, width=70)
root.mainloop()