1

Here is my goal:

Create a script that accept 2 CSV files and returns a 3rd CSV file containing only the differences.

I am using Python 3.7.2.

Here is my code:

def findFile1():
    filename =  ## prompts the user to select a .csv file.
    with open(filename, 'r') as t1:
        fileone = t1.readlines()
    return fileone


def findFile2():
    filename =  ## prompts the user to select another .csv file.
    with open(filename, 'r') as t2:
        filetwo = t2.readlines()
    return filetwo


def saveFile():
    filename =  ## prompts the user to name and choose a location to save a new .csv file.
    fileone = findFile1() ##Here I would just like to readlines from the first CSV file. Not run the whole script again. 
    filetwo = findFile2() ##Here I would just like to readlines from the second CSV file. Not run the whole script again.
    with open(filename, 'w') as outFile:
        for line in filetwo:
            if line not in fileone:
                outFile.write(line)

All I want is to use the return values from the first 2 functions, not call the whole function again.

UPDATE: I was able to solve this with Charles Duffy's suggestion "import functools and put the line @functools.lru_cache() above your functions, and all future calls will reuse the prior invocation's results"

Jacob Crocker
  • 75
  • 1
  • 7
  • Surely you can come up with a more succinct demonstration of your underlying question? I don't see why tkinter is necessary for a question about return values / calling conventions (and with all the scope that's beyond the title, it's not obvious what the code has to do with the stated question at all); note that per [mcve] guidelines, we ask that the code in a question be the *shortest possible* working example that demonstrates a specific, narrow problem. – Charles Duffy Feb 27 '19 at 01:40
  • ...that said, it sounds to me like what you want to do is *memoize* the results of your function calls, so they're remembered and reused on additional invocations. "memoization" is a term of art that it may be handy to search by. – Charles Duffy Feb 27 '19 at 01:42
  • Charles, please forgive me. This is my first time posting a question to this site. Please be patient with me as I learn the proper etiquette. Additionally, I have been learning python for about a week, so I'm learning what code is actually necessary to convey my question. Thank you for pointing me in the direction of memoization. I will chase that trail down and see where it leads me. – Jacob Crocker Feb 27 '19 at 02:30
  • I have made some edits to the question that I hope will make is clearer. Thank you for your suggestion. – Jacob Crocker Feb 27 '19 at 02:51
  • If you design your program like this, you are bound to run your function at some point of time. There is nothing wrong to define the return value from the function as a variable during `saveFile`. If you want to run it earlier then you have to define global variables to store them, or create a class instead and store them as attributes. – Henry Yik Feb 27 '19 at 03:30
  • Which version of Python? (In 3.2, `import functools` and put the line `@functools.lru_cache()` above your functions, and all future calls will reuse the prior invocation's results). See also [What is memoization and how can I use it in Python?](https://stackoverflow.com/questions/1988804/what-is-memoization-and-how-can-i-use-it-in-python) – Charles Duffy Feb 27 '19 at 14:39

2 Answers2

1

UPDATE: I was able to solve this with Charles Duffy's suggestion "import functools and put the line @functools.lru_cache() above your functions, and all future calls will reuse the prior invocation's results"

Jacob Crocker
  • 75
  • 1
  • 7
0

without enough information I'm doing some guess here: also I'm not familiar with tkinter:

here is a link for some help on passing parameters to your functions with Button How to pass arguments to a Button command in Tkinter?

from tkinter import filedialog
from tkinter import messagebox
from tkinter import *

master = Tk()

def findFile1():
    master.filename =  filedialog.askopenfilename(initialdir = "C:/",title = "Select file 1",filetypes = (("CSV","*.csv"),("all files","*.*")))
    print (master.filename)
    with open(master.filename, 'r') as t1:
        fileone = t1.readlines()
    return fileone


def findFile2():
    master.filename =  filedialog.askopenfilename(initialdir = "C:/",title = "Select file 2",filetypes = (("CSV","*.csv"),("all files","*.*")))
    print (master.filename)
    with open(master.filename, 'r') as t2:
        filetwo = t2.readlines()
    return filetwo


def saveFile(fileone, filetwo):
    master.filename =  filedialog.asksaveasfilename(initialdir = "C:/",title = "Save file",filetypes = (("CSV","*.csv"),("all files","*.*")))
    print (master.filename)
    with open(master.filename, 'w') as outFile:
        for line in filetwo:
            if line not in fileone:
                outFile.write(','.join(line.split()))
    messagebox.showinfo("Sucess", "File created successfully!")


file1 = Button(master, text="Load file 1", command=findFile1)
file1.pack()

file2 = Button(master, text="Load file 2", command=findFile2)
file2.pack()

start = Button(master, text="START", command=saveFile)
start.pack()

master.geometry("300x300")

mainloop()

Aiden Zhao
  • 633
  • 4
  • 15