-2

I have used os.walk() to get the list of files to a DataFrame. Now I want to extract the zip folders from the list of files in the DataFrame.

DataFrame

file_name   base_name extension    absolute_path                  rel_path
file_1.pdf  file_1     pdf      C:\\temp\documents\file_1.pdf   \file_1.pdf
file_2.zip  file_2     zip      C:\\temp\documents\file_2.zip   \file_2.zip
file_3.7z   file_3     7z       C:\\temp\documents\file_3.7z    \file_3.7z
file_4.tar  file_4     tar      C:\\temp\documents\file_4.tar   \file_4.tar


  1. I am using python shutil package for extracting/unzipping the contents. How can I do that?
  2. I am also looking for other file formats (.7z, .tar) as well in the same directory if they are available.
  3. The extracted folders should be in the same directory with the same name as well.

Note: cant change the package used for the extraction. only shutil package.

Deepak Harish
  • 123
  • 2
  • 7
  • okay, i will reiterate my issue. The location of the files have been traced to a dataframe. The dataframe contains absolute paths and relative path of that particular file. Now I want to perform a recursion kind of to extract only the files with zip as extension to another folder in the same directory. – Deepak Harish Nov 12 '19 at 06:45
  • I changed the entire code to allow the unzip of an entire folder as well as a single compressed file – powerPixie Nov 14 '19 at 09:15
  • Where is your code? What is the problem you’re facing? – Abhijit Sarkar Jun 05 '23 at 21:04

2 Answers2

1

The alternative to open zipped folders is the ZiFile. You must install the library with pip install or any other installer (conda, for instance).

The import list is for the code is

import os
import fnmatch  
from zipfile import ZipFile

New code:

dirPath = 'C:\\temp' #Windows format
formats = ['*.zip','*.tar','*.7z']

for f in formats:
    for file in os.listdir(dirPath):
        if fnmatch.fnmatch(file,f):
            os.chdir(dirPath) #change where to open zipFile
            with ZipFile(file,'r') as zfiles:
                flist = zfiles.namelist()
                for zipped in flist:
                    zfiles.extract(zipped,dirPath) 

If you want to extract to a different dir, change the variable dirPath in the line:

zfiles.extract(zipped,dirPath)
powerPixie
  • 718
  • 9
  • 20
  • it didnt work. When I am trying to convert a zip file, I got an error "ReadError: zsnh3-15s-0003_0_bl.zip is not a zip file". But when I tried using the same file to convert with shutil directly, it worked well. Also fformat `for f in fformat` is same as format _format_ variable #type of files to unzipping. – Deepak Harish Nov 12 '19 at 06:50
  • check the new comment I added. – Deepak Harish Nov 12 '19 at 06:51
  • 1
    @DeepakHarish Check the new code I made to unzip files and folders as well – powerPixie Nov 14 '19 at 19:02
  • 1
    @DeepakHarish Have you seen the new code? Is it helpful? – powerPixie Nov 15 '19 at 12:56
-1
import zipfile

zip_content = zipfile.ZipFile("zip file path")

namelist = zip_content.namelist()

file_extracted = ""

for item in namelist:

   if "exact_file_name.ext" in item:

      file_extracted = zip_content.open(str(item))

try:

     data_content = file_extracted.read()

     fileout = open("path to save", 'wb')

     fileout.write(data_content)

     fileout.close()

 except:

     pass
Chamara
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 06 '23 at 03:54