tags:

views:

7

answers:

2

I want to add suffix to names of my files, for example uuid. How can i extract files using zipfile and pass custom names?

A: 

Step 1: Extract the files.

Step 2: Rename them.

katrielalex
I can't, because there is possibility to overwrite other files, which don't have uuid.
SuitUp
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
And if it exist?
SuitUp
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
+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