I unpack a large number of archives containing files with the same names.
They are now overwriting each other. Please help me understand how to make the code so that when the file is unpacked, the copy will receive a unique name and not be overwritten.
for fz in os.listdir(tmp_path):
if fz.endswith('.zip'):
with zf(tmp_path+fz, 'r') as z:
members = z.namelist()
for member in members:
z.extract(member, tmp_path)
You can identify the condition (the file exist) simply by using os.path.exists()
(see os.path). If that is the case, you might want to use Zipfile.read
to read the file to memory and then write it out using normal file operations, but to a new name.