{"id":4806,"date":"2025-01-22T10:37:40","date_gmt":"2025-01-22T10:37:40","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4806"},"modified":"2025-05-20T21:16:08","modified_gmt":"2025-05-20T20:16:08","slug":"working-with-zip-files","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/file-input-and-output\/zip-files\/working-with-zip-files","title":{"rendered":"Working with zip files"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>from zipfile import ZipFile \nimport os<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Extract zip file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    with ZipFile(\"myzipfile.zip\", 'r') as zip_object: \n\n        if \"text1.txt\" in zip_object.namelist():\n            zip_object.extract(\"text1.txt\", path=Path(\"C:\/my_folder\/temp\/\"))     #Extracting a specific file in the zip to a specific location (trailing slash on path is optional)\n\n        #zip_object.extractall(path=Path(\"C:\/my_folder\/temp\"))            #If you don't provide a path is will extract to the current folder\n        #WARNING - if the zip is provided externally are you sure there won't be an attack vector created using extractall() by files you're not expecting being included in the zip or files marked with a specific extract path not being picked up and blocked by the extractall() function?  \n\n        zip_object.close()<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Create zip file<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>    with ZipFile(LIGHTING_IMPORT_FILE_NAME, 'w') as zip_object:\n        # Adding files that need to be zipped\n        zip_object.write(\"text1.txt\")\n        zip_object.write(\"spreadsheet1.csv\")\n\n    # Check to see if the zip file is created\n    if os.path.exists(LIGHTING_IMPORT_FILE_NAME):\n        print(\"New lighting file create with changes: \" + LIGHTING_IMPORT_FILE_NAME)\n    else:\n        print(\"An error occured, couldn't create new zip file\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Extract zip file Create zip file<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[],"class_list":["post-4806","post","type-post","status-publish","format-standard","hentry","category-zip-files"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4806"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4806\/revisions"}],"predecessor-version":[{"id":4948,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4806\/revisions\/4948"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}