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