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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
SuitUp
  • 3,112
  • 5
  • 28
  • 41

2 Answers2

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())`, or bypass the entire problem by putting each new zip file in a UUID-named directory of its own. – Katriel Oct 20 '10 at 21:30