0

With this code, I extract the files from the zip file, but if there is a file with the same name as the files I extracted in a different zip file, it overwrites this file. I want this file to be extracted by adding the "_1" to the end. Can you help me with this?

import zipfile, os, shutil
myZip = zipfile.ZipFile('abc.zip')    

directory_to_extract_to = "place of extraction path"

for file in myZip.filelist:
    if file.filename.endswith('.txt'):
        source = myZip.open(file)
        target = open(os.path.join(directory_to_extract_to, os.path.basename(file.filename)), "wb")

        with source, target:
            shutil.copyfileobj(source, target)

e.g; okay.txt >>> extract new file with the same name >>> okay_1.txt >>> okay_2.txt

  • Your best course would be to rename the zip file **before** unzipping it, then using `extractall` method on the `ZipFile` object. – Nastor Jul 28 '21 at 08:57
  • this does not solve it. – Muratcan Çoban Jul 28 '21 at 09:08
  • Why shouldn't this solve it? Your problem is having duplicate filenames... – Nastor Jul 28 '21 at 09:10
  • 1
    I have a lot of zip files and with this code I extract the text files inside these zip files and put them in a file. Some zip files can have the same text file name. If a file named "okay.txt" is extracted and there is a file called "okay.txt" in another zip file, I want it to be output as "okay_1.txt" this time. This code doesn't do that, it updates the "okay.txt" file. Your solution so I couldn't relate it exactly to this problem. – Muratcan Çoban Jul 28 '21 at 09:17
  • Does this answer your question? [renaming the extracted file from zipfile](https://stackoverflow.com/questions/44079913/renaming-the-extracted-file-from-zipfile) – Maurice Meyer Jul 28 '21 at 09:42
  • You can [edit] your question from here. Add image of your destination folder and files of zip file. – imxitiz Jul 28 '21 at 11:17
  • @MuratcanÇoban you can [edit] your question by clicking edit word! – imxitiz Jul 28 '21 at 11:26
  • why would i do this? – Muratcan Çoban Jul 28 '21 at 11:39
  • Cuz that is not an answer, you are providing resources for me to solve your issue – imxitiz Jul 28 '21 at 11:39

1 Answers1

0

I believe this will answer your question:

import zipfile, os, shutil

myZip = zipfile.ZipFile('abc.zip') 

directory_to_extract_to = "place of extraction path"

for i,file in enumerate(myZip.filelist):
    if file.filename.endswith('.txt'):
        if file.filename not in os.listdir(directory_to_extract_to):
            path=os.path.join(directory_to_extract_to, os.path.basename(file.filename))
        else:
            path=os.path.join(directory_to_extract_to, os.path.basename(file.filename.replace(".txt",str(i)+".txt")))
        source = myZip.open(file)
        target = open(path, "wb")

        with source, target:
            shutil.copyfileobj(source, target)

What we are doing here is, searching file name in that extracting directory if found then add iteration number in file name otherwise same as what you are doing. We are using iteration number so, you may not get the continuous number in file name but I believe, it solves your issue.

imxitiz
  • 3,920
  • 3
  • 9
  • 33
  • @MuratcanÇoban have you try to debug, anything found while debugging? Which is not expected? I haven't tested it. I will test it and inform you! Stay connected. :) – imxitiz Jul 28 '21 at 11:07
  • your code gave no errors. it did the same with the code I wrote. – Muratcan Çoban Jul 28 '21 at 11:09
  • @MuratcanÇoban please post the image of your destination directory and your zip file! Cuz it is working perfectly fine for me. As I expect. You can add image in question! Don't post image as answer. – imxitiz Jul 28 '21 at 11:15