To delete a file from a ZIP archive using Python's zipfile module, you'll need to create a new ZIP archive that excludes the file you want to delete. Python's zipfile module doesn't provide a direct method for deleting files from an existing archive. Here's how you can do it:
import zipfile
import os
# Specify the path to the existing ZIP file
zip_file_path = 'my_archive.zip'
# Specify the name of the file you want to delete from the ZIP archive
file_to_delete = 'file_to_delete.txt'
# Create a temporary file to hold the modified ZIP archive
temp_zip_path = 'temp_archive.zip'
# Open the existing ZIP file and create a new one without the file to delete
with zipfile.ZipFile(zip_file_path, 'r') as input_zip, zipfile.ZipFile(temp_zip_path, 'w') as output_zip:
for item in input_zip.infolist():
# Skip the file you want to delete
if item.filename != file_to_delete:
output_zip.writestr(item, input_zip.read(item.filename))
# Replace the original ZIP file with the modified one (if needed)
os.remove(zip_file_path)
os.rename(temp_zip_path, zip_file_path)
In this code:
Specify the path to the existing ZIP file (zip_file_path) and the name of the file you want to delete from the ZIP archive (file_to_delete).
Create a temporary ZIP file (temp_zip_path) to hold the modified archive.
Open the existing ZIP file for reading ('r') and the temporary ZIP file for writing ('w').
Iterate over the items (files and directories) in the existing ZIP archive using input_zip.infolist(). Copy all items to the new archive except for the file you want to delete.
After the loop, close both the input and output ZIP files.
Remove the original ZIP file (os.remove(zip_file_path)) and rename the temporary file to replace the original (os.rename(temp_zip_path, zip_file_path)).
After running this code, the specified file will be removed from the ZIP archive, and you'll have the modified archive in zip_file_path.
How to delete a file from a zip file using Python ZipFile module?
ZipFile module.import zipfile
import os
def delete_file_from_zip(zip_file_path, file_to_delete):
with zipfile.ZipFile(zip_file_path, 'a') as zf:
# Create a temporary file to copy contents excluding the file to delete
temp_file = zipfile.ZipFile("temp.zip", 'w')
for item in zf.infolist():
if item.filename != file_to_delete:
temp_file.writestr(item, zf.read(item.filename))
temp_file.close()
# Replace the original zip file with the temporary one
os.replace("temp.zip", zip_file_path)
Python ZipFile module delete file from archive example
ZipFile module.import zipfile
def delete_file_from_zip(zip_file_path, file_to_delete):
with zipfile.ZipFile(zip_file_path, 'a') as zf:
del zf[file_to_delete]
Remove file from zip with Python ZipFile module
ZipFile module in Python.import zipfile
def delete_file_from_zip(zip_file_path, file_to_delete):
with zipfile.ZipFile(zip_file_path, 'a') as zf:
zf.extractall(path="temp_extract")
zf.close()
with zipfile.ZipFile(zip_file_path, 'w') as new_zip:
for folderName, subfolders, filenames in os.walk("temp_extract"):
for filename in filenames:
if filename != file_to_delete:
filePath = os.path.join(folderName, filename)
new_zip.write(filePath, os.path.relpath(filePath, "temp_extract"))
shutil.rmtree("temp_extract")
Python remove file from zip without extracting
import zipfile
import io
def delete_file_from_zip(zip_file_path, file_to_delete):
with zipfile.ZipFile(zip_file_path, 'r') as zin:
with zipfile.ZipFile("temp.zip", 'w') as zout:
for item in zin.infolist():
if item.filename != file_to_delete:
buffer = zin.read(item.filename)
zout.writestr(item, buffer)
# Replace the original zip file with the updated one
os.replace("temp.zip", zip_file_path)
Python delete file from zip folder
import zipfile
def delete_file_from_zip_folder(zip_file_path, folder_name, file_to_delete):
with zipfile.ZipFile(zip_file_path, 'a') as zf:
folder_path = folder_name + '/'
file_path = folder_path + file_to_delete
del zf[file_path]
How to exclude a file when zipping in Python?
import zipfile
def create_zip_excluding_file(zip_file_path, files_to_zip, file_to_exclude):
with zipfile.ZipFile(zip_file_path, 'w') as zf:
for file in files_to_zip:
if file != file_to_exclude:
zf.write(file)
Python remove specific file from zip archive
import zipfile
def delete_file_from_zip(zip_file_path, file_to_delete):
with zipfile.ZipFile(zip_file_path, 'a') as zf:
del zf[file_to_delete]
How to delete files from zip in Python programmatically?
import zipfile
def delete_files_from_zip(zip_file_path, files_to_delete):
with zipfile.ZipFile(zip_file_path, 'a') as zf:
for file in files_to_delete:
del zf[file]
Python ZipFile module delete file from zip without extracting
ZipFile module without extracting its contents.import zipfile
def delete_file_from_zip(zip_file_path, file_to_delete):
with zipfile.ZipFile(zip_file_path, 'r') as zin:
with zipfile.ZipFile("temp.zip", 'w') as zout:
for item in zin.infolist():
if item.filename != file_to_delete:
buffer = zin.read(item.filename)
zout.writestr(item, buffer)
os.replace("temp.zip", zip_file_path)
Delete file from zip using Python ZipFile module without temporary files
recyclerview-layout bots documentfile sparse-matrix var timeout proximitysensor android-camera2 materialize optionmenu