Home > database >  Concatenation two files in python
Concatenation two files in python

Time:05-04

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 ?

CodePudding user response:

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

CodePudding user response:

you can use something like this

with open('<file1>') as f1, open('<file2>')  as f2, open('file3') as f3:
    # do the magic

CodePudding user response:

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

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

Here is the link to the documentation of concat.

Cheers!

  • Related