I want to unzip files with python 2.7.8 .When i try to extract zip files that contain files with same names in it to one folder, some files got lost because duplication names. I try that:
import zipfile,fnmatch,os
rootPath = r"C:\zip"
pattern = '*.zip'
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
print(os.path.join(root, filename))
outpath = r"C:\Project\new"
zipfile.ZipFile(os.path.join(root, filename)).extractall(r"C:\Project\new")
UPDATE:
I try to extract all the files located inside the zip files into one folder only without a new subfolders created. If there are files with the same name i need all the files
The
ZipFile.extractall()
method simply extracts the files and stores them one by one in the target path. If you want to preserve files with duplicated names you will have to iterate over the members usingZipeFile.namelist()
and take appropriate action when you detect duplicates. TheZipFile.read()
allows you to read the file contents, then you can write them wherever (and with whatever name) you want.