Yahyaotaif
Yahyaotaif

Reputation: 1973

Concatenation two files in python

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 ?

Upvotes: 0

Views: 162

Answers (2)

lowkey
lowkey

Reputation: 140

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!

Upvotes: 1

fireshadow52
fireshadow52

Reputation: 6516

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

Upvotes: 0

Related Questions