Search code examples
pythonmergeziptxt

How to I merge multiple .txt files that are in a .zip file into only one .txt file in Python?


I'm trying to merge multiple .txt files that are in a .zip file into only one .txt file in Python.

My code is the following:

firstfile = Path(r'C:\Users\Viniz\Downloads\devkmbe-5511001_05-12-2022_00_20_09.zip\AudioCaptureMemoryUsage_01_12_2022.txt')
secondfile = Path(r'C:\Users\Viniz\Downloads\devkmbe-5511001_05-12-2022_00_20_09.zip\AudioMatchingMemoryUsage_01_12_2022.txt')

newfile = input("Enter the name of the new file: ")
print()
print("The merged content of the 2 files will be in", newfile)

with open(newfile, "wb") as wfd:
    for f in [firstfile, secondfile]:
        with open(f, "rb") as fd:
            shutil.copyfileobj(fd, wfd, 1024 * 1024 * 10)

print("\nThe content is merged successfully.!")
print("Do you want to view it ? (y / n): ")

check = input()
if check == 'n':
    exit()
else:
    print()
    c = open(newfile, "r")
    print(c.read())
    c.close()



Thanks.

I tried to merge them in only one file but it doesn't worked.


Solution

  • To merge the files, you'll need to first extract the files from the zip file, then merge them, and then write the merged content to a new file. Here is an example of how you can do this using the zipfile module.

    Update: If the .txt files are located inside a folder within the zip file, you'll need to include the folder name in the path when opening the files.

    import zipfile
    
    zip_file = r'C:\Users\Viniz\Downloads\devkmbe-5511001_05-12-2022_00_20_09.zip'
    folder_name = 'myfolder'
    first_file = folder_name + '/AudioCaptureMemoryUsage_01_12_2022.txt'
    second_file = folder_name + '/AudioMatchingMemoryUsage_01_12_2022.txt'
    
    with zipfile.ZipFile(zip_file, 'r') as zip_ref:
        with zip_ref.open(first_file) as f1, zip_ref.open(second_file) as f2:
            first_content = f1.read()
            second_content = f2.read()
    
        # Concatenate the two files
        merged_content = first_content + second_content
        
        # Write the merged content to a new file
        new_file = input("Enter the name of the new file: ")
        with open(new_file, 'wb') as new_f:
            new_f.write(merged_content)
            
        print("The content is merged successfully.!")
        print("Do you want to view it ? (y / n): ")
    
        check = input()
        if check == 'n':
            exit()
        else:
            print()
            c = open(new_file, "r")
            print(c.read())
            c.close()
    

    Make sure to replace 'myfolder' with the actual name of the folder containing the .txt files in your zip file.

    For multiple files..

    import zipfile
    
    zip_file = r'C:\Users\Viniz\Downloads\devkmbe-5511001_05-12-2022_00_20_09.zip'
    folder_name = 'myfolder'
    file_names = ['AudioCaptureMemoryUsage_01_12_2022.txt',
                  'AudioMatchingMemoryUsage_01_12_2022.txt',
                  'File3.txt',
                  'File4.txt',
                  ...
                  'File29.txt']
    
    merged_content = b''  # Initialize an empty bytes object
    with zipfile.ZipFile(zip_file, 'r') as zip_ref:
        for file_name in file_names:
            with zip_ref.open(folder_name + '/' + file_name) as f:
                merged_content += f.read()
                
        # Write the merged content to a new file
        new_file = input("Enter the name of the new file: ")
        with open(new_file, 'wb') as new_f:
            new_f.write(merged_content)
            
        print("The content is merged successfully.!")
        print("Do you want to view it ? (y / n): ")
    
        check = input()
        if check == 'n':
            exit()
        else:
            print()
            c = open(new_file, "r")
            print(c.read())
            c.close()