{"id":523,"date":"2021-05-06T16:39:09","date_gmt":"2021-05-06T07:39:09","guid":{"rendered":"https:\/\/shibaura-it.tokyo\/it-solution\/?p=523"},"modified":"2021-05-06T16:39:09","modified_gmt":"2021-05-06T07:39:09","slug":"python%e3%81%a7%e3%80%81%e6%8c%87%e5%ae%9a%e3%81%97%e3%81%9f%e9%9a%8e%e5%b1%a4%e4%bb%a5%e4%b8%8b%e3%82%92zip%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/shibaura-it.tokyo\/it-solution\/python%e3%81%a7%e3%80%81%e6%8c%87%e5%ae%9a%e3%81%97%e3%81%9f%e9%9a%8e%e5%b1%a4%e4%bb%a5%e4%b8%8b%e3%82%92zip%e3%81%99%e3%82%8b\/","title":{"rendered":"Python\u3067\u3001\u6307\u5b9a\u3057\u305f\u968e\u5c64\u4ee5\u4e0b\u3092zip\u3059\u308b"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f<\/p>\n<p>\u524d\u56de\u8a18\u4e8b\u3000<a href=\"https:\/\/shibaura-it.tokyo\/it-solution\/python-\u3092\u5229\u7528\u3057\u3066\u30d5\u30a1\u30a4\u30eb\u3092\u5727\u7e2e\u3059\u308b\/\">\u201dpython\u3092\u5229\u7528\u3057\u3066\u30d5\u30a1\u30a4\u30eb\u3092\u5727\u7e2e\u3059\u308b\u201d<\/a>\u3000\u306e\u554f\u984c\u70b92\u306b\u3064\u3044\u3066\u306e\u3001\u79c1\u306e\u89e3\u6c7a\u7b56\u3067\u3059\u3002<\/p>\n<p>\u5177\u4f53\u7684\u306b\u306f\u3001&#8221;os.walk~ &#8220;\u3067\u30eb\u30fc\u30d7\u306b\u7a4d\u307e\u308c\u308b\u6587\u5b57\u6570(\u30d9\u30fc\u30b9\u306eFullPath\uff0bSubFolder)\u3068\u3001<br \/>\n\u5bfe\u8c61\u306e\u30d9\u30fc\u30b9\u3068\u306a\u308bFullPath\u306e\u30d5\u30a9\u30eb\u30c0\u30fc\u540d\u306e\u6587\u5b57\u6570\u304c\u7570\u306a\u308b\u3053\u3068\u3092\u5229\u7528\u3057\u3066\u3001<br \/>\n\u30d5\u30a9\u30eb\u30c0\u306e\u5727\u7e2e\u5bfe\u8c61\u306e\u5224\u5b9a\u3068\u3001\u5727\u7e2e\u6642\u306e\u30d5\u30a9\u30eb\u30c0\u69cb\u6210\u3067\u5bfe\u7b56\u3092\u65bd\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:python decode:true \" title=\"ZipSample2.py\">import os, zipfile\r\n\r\n#   TargetPath\u3067\u6307\u5b9a\u3055\u308c\u305f\u30d5\u30a9\u30eb\u30c0\u914d\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u30d5\u30a9\u30eb\u30c0\u4ed8\u304d\u3067ZIP\u3057\u307e\u3059\u3002\r\ndef ZipAll(TargetPath):\r\n\r\n    with zipfile.ZipFile('test.zip','w') as myzip:\r\n\r\n        #   os.walk\u3067TargetPath\u5185\u306e\u30d5\u30a9\u30eb\u30c0\u3092\u9806\u306b\u8d70\u67fb\r\n        for folder, subfolders, files in os.walk(TargetPath):\r\n\r\n            os.chdir(TargetPath)                #   \u30ab\u30ec\u30f3\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u79fb\u52d5\u3059\u308b\r\n            lenTarget = len(TargetPath)+1       #   \u6307\u5b9a\u3055\u308c\u305fFulPath\u30d5\u30a9\u30eb\u30c0\u540d\u306e\u6587\u5b57\u6570\u3092\u30ab\u30a6\u30f3\u30c8\r\n            lenFolder = len(folder)             #   \u7e70\u308a\u8fd4\u3057\u4e2d\u306e\u30d5\u30a9\u30eb\u30c0\u540d\u306e\u6587\u5b57\u6570\u3092\u30ab\u30a6\u30f3\u30c8\r\n\r\n            #   lenTarget\u3068lenFolder\u306e\u6587\u5b57\u6570\u304c\u7570\u306a\u308c\u3070SubFolder\u306a\u306e\u3067\u5727\u7e2e\u5bfe\u8c61\u3068\u3059\u308b\r\n            if (lenTarget &lt; lenFolder):\r\n\r\n                #   \u30d5\u30a9\u30eb\u30c0\u3092Zip\u683c\u7d0d\u5bfe\u8c61\u3068\u3057\u3066\u66f8\u304d\u8fbc\u3080\r\n                #   \u5bfe\u8c61\u3068\u306a\u308b\u306e\u306fFullPath\u3067\u306f\u306a\u304f\u30ab\u30ec\u30f3\u30c8\u30d5\u30a9\u30eb\u30c0\u4ee5\u964d\u306e\u307f\r\n                myzip.write(folder[lenTarget:lenFolder])    \r\n        \r\n                #   \u30d5\u30a9\u30eb\u30c0\u5185\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u8d70\u67fb\r\n                for file in files:\r\n\r\n                    #   \u30d5\u30a1\u30a4\u30eb\u3092Zip\u683c\u7d0d\u5bfe\u8c61\u3068\u3057\u3066\u66f8\u304d\u8fbc\u3080\r\n                    myzip.write(os.path.join(folder[lenTarget:lenFolder],file))\r\n\r\nif __name__ == '__main__':\r\n    ZipAll('\/Users\/FolderSample\/test')<\/pre>\n<p>&nbsp;<\/p>\n<p>\u4e0a\u8a18\u3092\u5b9f\u884c\u3059\u308b\u3068&#8230;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-thumbnail wp-image-518\" src=\"https:\/\/shibaura-it.tokyo\/it-solution\/wp\/wp-content\/uploads\/2021\/05\/717242f64d8b35b62e5e81e917c51a7b-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" srcset=\"https:\/\/shibaura-it.tokyo\/it-solution\/wp\/wp-content\/uploads\/2021\/05\/717242f64d8b35b62e5e81e917c51a7b-150x150.jpg 150w, https:\/\/shibaura-it.tokyo\/it-solution\/wp\/wp-content\/uploads\/2021\/05\/717242f64d8b35b62e5e81e917c51a7b-80x80.jpg 80w, https:\/\/shibaura-it.tokyo\/it-solution\/wp\/wp-content\/uploads\/2021\/05\/717242f64d8b35b62e5e81e917c51a7b-320x320.jpg 320w\" sizes=\"(max-width: 150px) 100vw, 150px\" \/><\/p>\n<p>\u7121\u4e8b\u56de\u7b54\u3055\u308c\u308b\u30d5\u30a1\u30a4\u30eb\u69cb\u9020\u304c&#8230;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-thumbnail wp-image-520\" src=\"https:\/\/shibaura-it.tokyo\/it-solution\/wp\/wp-content\/uploads\/2021\/05\/717242f64d8b35b62e5e81e917c51a7b-1-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" srcset=\"https:\/\/shibaura-it.tokyo\/it-solution\/wp\/wp-content\/uploads\/2021\/05\/717242f64d8b35b62e5e81e917c51a7b-1-150x150.jpg 150w, https:\/\/shibaura-it.tokyo\/it-solution\/wp\/wp-content\/uploads\/2021\/05\/717242f64d8b35b62e5e81e917c51a7b-1-80x80.jpg 80w, https:\/\/shibaura-it.tokyo\/it-solution\/wp\/wp-content\/uploads\/2021\/05\/717242f64d8b35b62e5e81e917c51a7b-1-320x320.jpg 320w\" sizes=\"(max-width: 150px) 100vw, 150px\" \/><\/p>\n<p>\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u6b21\u56de\u306f\u30011\u306e\u554f\u984c\u89e3\u6c7a\u6cd5\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f \u524d\u56de\u8a18\u4e8b\u3000\u201dpython\u3092\u5229\u7528\u3057\u3066\u30d5\u30a1\u30a4\u30eb\u3092\u5727\u7e2e\u3059\u308b\u201d\u3000&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":227,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/shibaura-it.tokyo\/it-solution\/wp-json\/wp\/v2\/posts\/523"}],"collection":[{"href":"https:\/\/shibaura-it.tokyo\/it-solution\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shibaura-it.tokyo\/it-solution\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shibaura-it.tokyo\/it-solution\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shibaura-it.tokyo\/it-solution\/wp-json\/wp\/v2\/comments?post=523"}],"version-history":[{"count":1,"href":"https:\/\/shibaura-it.tokyo\/it-solution\/wp-json\/wp\/v2\/posts\/523\/revisions"}],"predecessor-version":[{"id":524,"href":"https:\/\/shibaura-it.tokyo\/it-solution\/wp-json\/wp\/v2\/posts\/523\/revisions\/524"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/shibaura-it.tokyo\/it-solution\/wp-json\/wp\/v2\/media\/227"}],"wp:attachment":[{"href":"https:\/\/shibaura-it.tokyo\/it-solution\/wp-json\/wp\/v2\/media?parent=523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shibaura-it.tokyo\/it-solution\/wp-json\/wp\/v2\/categories?post=523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shibaura-it.tokyo\/it-solution\/wp-json\/wp\/v2\/tags?post=523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}