I'm struggling with unzip process with this code:
I have two separated .zip files and each has the same file name and file type, but when I execute this code only appears one file extracted, instead of two.
This is the result:
Code:
import os, zipfile
dir_name = 'C:\\Users\\Efste\\Desktop\\Test'
extension = ".zip"
os.chdir(dir_name) # change directory from working dir to dir with files
for item in os.listdir(dir_name):
if item.endswith(extension):
file_name = os.path.abspath(item)
with zipfile.ZipFile(file_name, 'r') as zipObj:
listOfFileNames = zipObj.namelist()
for fileName in listOfFileNames:
zipObj.extract(fileName)
zipObj.extract(fileName, os.path.basename(item).replace('.zip',''))
What I need is to keep both files by adding an incremental number to the files that are duplicated.