I want to add suffix to names of my files, for example uuid. How can i extract files using zipfile and pass custom names?
Asked
Active
Viewed 2,379 times
2 Answers
4
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
- 776,304
- 153
- 1,341
- 1,358
0
Step 1: Extract the files.
Step 2: Rename them.

Katriel
- 120,462
- 19
- 136
- 170
-
I can't, because there is possibility to overwrite other files, which don't have uuid. – SuitUp Oct 20 '10 at 20:51
-
Then check if there are other files before you rename? Unless there are some more complications here I don't really see the problem... – Katriel Oct 20 '10 at 21: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(
,"wb").write(archive.extract( – Katriel Oct 20 '10 at 21:30))`, or bypass the entire problem by putting each new zip file in a UUID-named directory of its own.