Python Forum
Python script that recursively zips folders WITHOUT nesting the folder inside the zip
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python script that recursively zips folders WITHOUT nesting the folder inside the zip
#1
Okay I will try to explain this as best as I can. My goal is to create a python script that will loop through folders, zip them up, but NOT nest the folder inside the zip file. For example: the zip file structure cannot produce Cat.zip/Cat/catnip.jpg but produces Cat.zip/catnip.jpg.

There is a parent Folder called:
Original_Raw_Data
--620_PANO_1_20190802 #subfolder that need zipped
-contents
--787_PANO_1_20190802 #subfolder that need zipped
--788_PANO_1_20190802 #subfolder that need zipped

When you zip a folder manually the structure goes
788_PANO_1_20190802.ip/788_PANO_1_20190802/file.txt
I am trying
788_PANO_1_20190802.zip/file.txt

Currently my code is as follows
Any help would be appreciated!
>>> import os
>>> import zipfile
>>> zf = zipfile.ZipFile("620_PANO_1_20190802.zip", "w")
>>> for dirname, subdirs, files in os.walk("P:\Standards\Tools_and_Scripts\ZipFolderTools\Original_Raw_Data\620_PANO_1_20190802"):
	for filename in files:
		zf.write(os.path.join('620_PANO_1_20190802', filename)
    zf.close()
Reply
#2
(Feb-11-2020, 04:27 PM)umkc1 Wrote: for filename in files:
        zf.write(os.path.join('620_PANO_1_20190802', filename)

The documentation says to add the argument "arcname" to the .write() line. Otherwise your filename is used by default and the directory is included.

maybe try this where arcname is the name you want to give the archive:
 for filename in files:
        zf.write(os.path.join('620_PANO_1_20190802', filename, arcname)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Folders Splitter to Files Splitter Guybrush16bit 12 1,368 Mar-01-2025, 08:17 PM
Last Post: snippsat
  Modifying a dictionary recursively SpongeB0B 2 1,209 May-12-2024, 04:09 PM
Last Post: Gribouillis
Question How to add Python folder in Windows Registry ? Touktouk 1 1,221 Feb-20-2024, 01:04 PM
Last Post: DeaD_EyE
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 1,275 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 1,479 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  What script to paste folders thenewcoder 1 1,307 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 8,060 Jun-29-2023, 11:57 AM
Last Post: gologica
  python move folders and subfolders not working mg24 5 4,358 Nov-09-2022, 02:24 PM
Last Post: Larz60+
  python gzip all files from a folder mg24 3 6,684 Oct-28-2022, 03:59 PM
Last Post: mg24
  Make a python folder .exe Extra 0 1,496 Jun-10-2022, 01:20 AM
Last Post: Extra

Forum Jump:

User Panel Messages

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