1

To rename the one file in a ZipFile that I'm downloading, I do the following:

for item in zipfile.infolist():
    old_name =  item.filename
    match = re.search(r'(.*)(.mdb)', item.filename)
    item.filename = "%s_friend%s" % (match.group(1),, match.group(2)) # I should probably be using replace here
    zipfile.extract(old_name, save_dir)

However when I want to extract that file and save it into a specific directory, I need to reference the "old_name" and cannot reference the new. Is there a "cleaner" way of both extracting the renamed file? Or is it more pythonic to first extract and then rename the file?

Like the OP of this SO question, I run into the same error when referencing the renamed file.

updated: This isn't correctly updating the first file. Though it appears to rename the file properly, it output the originally named file.

for item in zipfile.infolist():
    old_name =  item.filename
    match = re.search(r'(.*)(.mdb)', item.filename)
    print match.group(1), match.group(2)
    item.filename = "%s_%s%s" % (match.group(1), year, match.group(2))
    print item.filename
zipfile.close()
with ZipFile(curr_zip, 'r') as zpf:
    for item in zpf.infolist():
        zpf.extract(item.filename, save_dir)
Dharman
  • 30,962
  • 25
  • 85
  • 135
NumenorForLife
  • 1,736
  • 8
  • 27
  • 55
  • Do you want to extract as you are going through each item? Or would it be fine outside the for loop, in another for loop ? If latter, do not extract in the same for loop you are renaming the file in. Instead close the zipfile using zipfile.close() and the reopen it and then extract the files – Anand S Kumar Jun 20 '15 at 17:47

1 Answers1

2

After testing found that it is not possible to directly rename the file inside a zip folder. All you can do would be to n create a completely new zipfile and add the files back to the new zip file using different name.

Example code for that -

source = ZipFile('source.zip', 'r')
target = ZipFile('target.zip', 'w', ZIP_DEFLATED)
for file in source.filelist:
    if not <filename_to_change>:
        target.writestr(file.filename, source.read(file.filename))
    else:
        target.writestr('newfilename', source.read(file.filename))
target.close()
source.close()
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • i haven't saved the file yet because I'm downloading it directly from a url ... zipfile = ZipFile(StringIO(url.read())) Do you recommend first saving it, and then renaming it? – NumenorForLife Jun 20 '15 at 17:56
  • Just save the `StringIO` object in a variable, it contains your full zipfile (in memory) and you can just wrap it in a `ZipFile` again.. – dhke Jun 20 '15 at 18:00
  • see above. it's not being found when I try to rename it. I get a KeyError – NumenorForLife Jun 20 '15 at 18:29
  • After doing a lot of testing/researching , I don't think its possible to rename the file inside the zip itself. All you would be able to do is change the directory/filename after extracting it, as you gave in the http://stackoverflow.com/questions/4917284/extract-files-from-zip-without-keeping-the-structure-using-python-zipfile question. – Anand S Kumar Jun 20 '15 at 18:33
  • How come that a programme like 7-zip allows us to rename a file inside a zip file and there seems to be no Python library that would allow us to do the same? – Dobedani Apr 25 '22 at 09:59