0

I have the following functions to read files, then store them inside variables:

def browseFiles():
    filename = filedialog.askopenfilename(initialdir = "/home",
                                          title = "Select a File",
                                          filetypes = (("Text files",
                                                        "*.docx*"),
                                                       ("all files",
                                                        "*.*")))
    label_file_explorer.configure(text="File Opened: "+ filename)
    with open(filename) as fp:
     firstfile = fp.read()

def browseFiles1():
    filename1 = filedialog.askopenfilename(initialdir = "/home",
                                          title = "Select a File",
                                          filetypes = (("Text files",
                                                        "*.docx*"),
                                                       ("all files",
                                                        "*.*")))
    label_file_explorer.configure(text="File Opened: "+ filename1)
    with open(filename1) as fp:
     secondfile = fp.read()

I want to concatenate firstfile and secondfile together, then produce a third file. So, I used :

firstfile += "\n"
firstfile += secondfile
  
with open ('thirdfile.docx', 'w') as fp:
    fp.write(firstfile)

My questions is how to access the variables firstfile and secondfile in each function and use them to produce a third file ?

Yahyaotaif
  • 1,943
  • 1
  • 15
  • 17
  • It is better to open the file in binary mode if it is a `.docx` file. But note that merging two `.docx` files is not the same as merging two text files. – acw1668 May 04 '22 at 10:41
  • Good point. Any link to opening a file in a binary mode would be helpful. – Yahyaotaif May 04 '22 at 13:34

2 Answers2

1

You have to return the firstfile and the secondfile from the 2 functions, store them in variables and then use the pd.concat function.

def browseFiles():
    filename = filedialog.askopenfilename(initialdir = "/home",
                                          title = "Select a File",
                                          filetypes = (("Text files",
                                                        "*.docx*"),
                                                       ("all files",
                                                        "*.*")))
    label_file_explorer.configure(text="File Opened: "+ filename)
    with open(filename) as fp:
     firstfile = fp.read()

    return firstfile


def browseFiles1():
    filename1 = filedialog.askopenfilename(initialdir = "/home",
                                          title = "Select a File",
                                          filetypes = (("Text files",
                                                        "*.docx*"),
                                                       ("all files",
                                                        "*.*")))
    label_file_explorer.configure(text="File Opened: "+ filename1)
    with open(filename1) as fp:
     secondfile = fp.read()
    
    return secondfile

firstfile = browseFiles()
secondfile = browseFiles1()

thirdfile = pd.concat([firstfile, secondfile])

Here is the link to the documentation of concat.

Cheers!

lowkey
  • 140
  • 10
0

You can return the file contents from the two functions, and that's how you'll access both from your "main function".

fireshadow52
  • 6,298
  • 2
  • 30
  • 46
  • Thats prolly not possible assuming the functions are callbacks triggered by a button, and `return` inside callbacks are quite useless – Delrius Euphoria May 04 '22 at 04:03