Search code examples
python-3.xziprenamepython-zipfile

Renaming the folder zip file extracts to


Might just be an edge case but I extracted a zip file to a directory using the zip file module. When extracting, zip file names the directory it extracts to.

If there is a way I get to specify the name of the folder Zip file creates to extract the files to? I am hitting an error because I am using the same folder zipped up to test zip file and it keeps using the old folder name which already exists so it throws an error. Here is my code:

orginalFolderName = jobFolder + name
      with zipfile.ZipFile(directory,"r") as zip_ref:
        zip_ref.extractall(jobFolder)
      os.rename(orginalFolderName, newFoldername)
      directory = newFoldername

Solution

  • with zipfile.ZipFile(filepath) as z:
         z.extractall(dest_folder)
    

    filepath - Complete path of zipfile

    dest_folder - destination folder