![]() |
Python script that recursively zips folders WITHOUT nesting the folder inside the zip - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python script that recursively zips folders WITHOUT nesting the folder inside the zip (/thread-24380.html) |
Python script that recursively zips folders WITHOUT nesting the folder inside the zip - umkc1 - Feb-11-2020 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() RE: Python script that recursively zips folders WITHOUT nesting the folder inside the zip - michael1789 - Feb-11-2020 (Feb-11-2020, 04:27 PM)umkc1 Wrote: for filename in files: 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) |