I want to add suffix to names of my files, for example uuid. How can i extract files using zipfile and pass custom names?
I can't, because there is possibility to overwrite other files, which don't have uuid.
SuitUp
2010-10-20 20:51:20
Then check if there are other files before you rename? Unless there are some more complications here I don't really see the problem...
katrielalex
2010-10-20 21:00:33
And if it exist?
SuitUp
2010-10-20 21:10:00
I don't think I understand your main question. If you want to extract the zip file to some folder and there's a file in said folder that has the same name as one in the zip file, there's going to be a name clash no matter how you extract the files. You can either write the new file yourself, say by `open(<new name>,"wb").write(archive.extract(<old name>))`, or bypass the entire problem by putting each new zip file in a UUID-named directory of its own.
katrielalex
2010-10-20 21:30:39
+1
A:
Use ZipFile.open()
to open a read-only file-like to the file data, then copy it to a write-only file with the correct name using shutil.copyfileobj()
.
Ignacio Vazquez-Abrams
2010-10-20 20:46:43