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
Question How to add Python folder in Windows Registry ? Touktouk 1 206 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 278 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 466 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  What script to paste folders thenewcoder 1 639 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,008 Jun-29-2023, 11:57 AM
Last Post: gologica
  python move folders and subfolders not working mg24 5 2,072 Nov-09-2022, 02:24 PM
Last Post: Larz60+
  python gzip all files from a folder mg24 3 3,812 Oct-28-2022, 03:59 PM
Last Post: mg24
  Make a python folder .exe Extra 0 999 Jun-10-2022, 01:20 AM
Last Post: Extra
  How to return the next page from json recursively? sandson 0 1,104 Apr-01-2022, 11:01 PM
Last Post: sandson
  Help Needed | Read Outlook email Recursively & download attachment Vinci141 1 4,018 Jan-07-2022, 07:38 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

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