Python Forum
[Solved by deanhystad] Create a zip file using zipfile library
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved by deanhystad] Create a zip file using zipfile library
#1
Peace
Hi everyone

when I create a zip file, it takes all the folders and adds them to the zip file
"user/files/programs/day44/img.png"
what I want is to store only the last folder
"day44/img.png"

with zipfile.ZipFile(f"{file_name}.zip", 'w') as f:
				for image_name in images_name_list:
					file = os.path.join(values['-SOURCEFOLDER-'],image_name)
					f.write(file)
in the images_name_list variable we have a list of image names and types:
img1.png,img2.png ...
values['-SOURCEFOLDER-'] is the folder path C:user/files/programs/day44
Reply
#2
To be clear, you want the path in the zip file to start with "day44/" instead of "C:user/files/programs/day44/".

One way to do this is change directory to ""C:user/files/programs/" and use a relative path when you write the file. In the code below I want to zip some files from the current working directory, but I want to include the name of the directory in the zip file (not the full path). Since I write the files using a relative path, they appear in the zip file as a relative path.
import os
import zipfile
from pathlib import Path

cwd = Path(os.getcwd())
with zipfile.ZipFile("test.zip", "w") as f:
    os.chdir(cwd.parent)  # Change directory to parent directory
    for image_name in [f"dice{n}.png" for n in range(6)]:
       # Write files to the zip file
        file = os.path.join(cwd.name, image_name)
        f.write(file)
os.chdir(cwd)  # Move back to starting directory.
Another approach is to set the name used for the folder using the arcname argument. The code below uses arcname to "create" a folder named "images/" in the zipfile. When I open the zipfile the toplevel is "images/" and when I click on "images/" I see the png files.
import os
import zipfile

with zipfile.ZipFile("test.zip", "w") as f:
    for image_name in [f"dice{n}.png" for n in range(6)]:
        file = os.path.join(image_name)
        f.write(file, arcname=os.path.join("images/", image_name))
DZ_Galaxy likes this post
Reply
#3
Thank you so much Smile
by adding the argument "arcname" solved the problem
arcname=os.path.join("images/", image_name)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using zipfile module - finding folders not files darter1010 2 264 Apr-06-2024, 07:22 AM
Last Post: Pedroski55
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 845 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Create Choices from .ods file columns cspower 3 601 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 2,886 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  Use PM4PY and create working file thomaskissas33 0 661 Nov-14-2023, 06:53 AM
Last Post: thomaskissas33
  Create csv file with 4 columns for process mining thomaskissas33 3 752 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  Better python library to create ER Diagram by using pandas data frames as tables klllmmm 0 1,122 Oct-19-2023, 01:01 PM
Last Post: klllmmm
  zipfile module error pseudo 3 770 Jun-30-2023, 03:52 PM
Last Post: Gribouillis
  Calling a function (which accesses a library) from another file mouse9095 4 819 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,127 Apr-06-2023, 11:15 AM
Last Post: AlphaInc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020