1

I have a directory on my system that contains ten zip files. Each zip file contains 1 text file. I want to write a Python script that unzips all of the files in the directory, and then concatenates all of the resulting (unzipped) files into a single file. How can I do this? So far, I have a script that is unzipping all of the files, but I am not sure how to go about adding the concatenation. Below is what I have.

import os, zipfile

dir_name = '/path/to/dir'
pattern = "my-pattern*.gz"

os.chdir(dir_name)  # change directory from working dir to dir with files

for item in os.listdir(dir_name):  # loop through items in dir
    if item == pattern:  # check for my pattern extension
        file_name = os.path.abspath(item)  # get full path of files
        zip_ref = zipfile.ZipFile(file_name)  # create zipfile object
        zip_ref.extractall(dir_name)  # extract file to dir
        zip_ref.close()  # close file
Ryan
  • 658
  • 8
  • 22
  • 1
    Should be able to make use of the many example answers at http://stackoverflow.com/questions/5509872/python-append-multiple-files-in-given-order-to-one-big-file – dbmitch Sep 10 '16 at 22:19

1 Answers1

2

You don't have to write the files to disk when you unzip them, Python can read the file directly from the zip. So, assuming you don't need anything except the concatenated result, replace your last two lines with:

for zipfile in zip_ref.namelist():
    with open('targetfile', 'a') as target:
        target.write(zip_ref.read(zipfile))
Tore Eschliman
  • 2,477
  • 10
  • 16