{"id":228,"date":"2018-09-11T11:43:26","date_gmt":"2018-09-11T03:43:26","guid":{"rendered":"http:\/\/www.yinyubo.cn\/?p=228"},"modified":"2022-05-17T09:50:30","modified_gmt":"2022-05-17T01:50:30","slug":"pythozip","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=228","title":{"rendered":"python-\u6279\u91cf\u628a\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u540c\u65f6\u538b\u7f29\u6210ZIP\u6587\u4ef6"},"content":{"rendered":"<ul>\n<li>\n<h1><strong>\u660e\u786e\u9700\u6c42\uff08\u538b\u7f29\u6210ZIP\u6587\u4ef6\uff0c\u6587\u4ef6\u4e2d\u65e2\u6709\u666e\u901a\u6587\u4ef6\uff0c\u53c8\u6709\u6587\u4ef6\u5939\uff09<\/strong><\/h1>\n<p>1.\u901a\u8fc7\u67d0\u79cd\u65b9\u5f0f\u83b7\u5f97\u4e00\u4e2a\u6587\u4ef6(\u6587\u4ef6\u5939)\u5217\u8868\u4f5c\u4e3a\u4e00\u4e2alist\uff08\u4f8b\u5982wxpython\u7684wx.FileDialog\u65b9\u6cd5\uff0c\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\u6211\u4eec\u8df3\u8fc7\u6587\u4ef6\u5939\u5217\u8868\u7684\u83b7\u53d6\u65b9\u6cd5\uff09\u3002<br \/>\n2.\u9009\u62e9\u4e00\u4e2a\u538b\u7f29\u6587\u4ef6\u7684\u8f93\u51fa\u76ee\u5f55\u548c\u538b\u7f29\u6587\u4ef6\u7684\u8f93\u51fa\u540d\u5b57(\u4e0b\u9762\u4ee3\u7801\u4e2d\u9009\u62e9\u8f93\u51fa\u9ed8\u8ba4\u8def\u5f84\u4e3a\u7a0b\u5e8f\u6839\u76ee\u5f55)<br \/>\n3.\u628a\u6587\u4ef6list\u91cc\u7684\u6587\u4ef6\uff0c\u5148\u7edf\u4e00\u653e\u5728\u4e00\u4e2a\u4e34\u65f6\u6587\u4ef6\u5939\u91cc\uff0c\u7136\u540e\u628a\u8be5\u4e34\u65f6\u6587\u4ef6\u5939\u538b\u7f29\u6210ZIP\u6587\u4ef6\uff0c\u6700\u540e\u5220\u6389\u4e34\u65f6\u6587\u4ef6\u5939<\/li>\n<li>\n<h1><strong>\u7a0b\u5e8f\u5173\u952e\u70b9<\/strong><\/h1>\n<\/li>\n<\/ul>\n<p>1.\u4f7f\u7528python\u7684shutil\u6a21\u5757\u3002\u62f7\u8d1d\u6587\u4ef6\u548c\u62f7\u8d1d\u6587\u4ef6\u5939\u7528\u7684\u662f\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u5728\u586b\u5199\u76ee\u6807\u6587\u4ef6\u5939\u65f6\uff0c\u6709\u5f88\u5927\u533a\u522b\uff0ccopytree()\u5982\u679c\u76ee\u6807\u6587\u4ef6\u5939\u8def\u5f84\u5df2\u5b58\u5728\uff0c\u4f1a\u5bfc\u81f4\u62f7\u8d1d\u5931\u8d25\uff0c\u6240\u4ee5copytree()\u7684\u76ee\u6807\u6587\u4ef6\u5939\u8def\u5f84\uff0c\u6211\u8bbe\u8ba1\u6210\u4e86\u76ee\u6807\u76ee\u5f55+\u539f\u6587\u4ef6\u5939\u540d\u79f0\u7684\u8def\u5f84<\/p>\n<pre class=\"\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">#\u62f7\u8d1d\u6587\u4ef6\uff0c\u8be5\u65b9\u6cd5\u7b2c\u4e8c\u4e2a\u53c2\u6570\u586b\u5199\u7684\u662f\u76ee\u6807\u76ee\u5f55\nshutil.copy2(srcfile, folder_name)<\/code><\/pre>\n<pre class=\"\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">#\u62f7\u8d1d\u6587\u4ef6\u5939\uff0c\u8be5\u65b9\u6cd5\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u662f\u76ee\u6807\u76ee\u5f55+\u539f\u6587\u4ef6\u5939\u540d\u79f0\uff0c\n#\u56e0\u4e3a\u5982\u679c\u76ee\u6807\u6587\u4ef6\u5939\u8def\u5f84\u5df2\u5b58\u5728\u4f1a\u5bfc\u81f4\u62f7\u8d1d\u5931\u8d25\nlast_name = os.path.basename(srcfile)\ndestination_name = folder_name + last_name\nshutil.copytree(srcfile, destination_name)<\/code><\/pre>\n<p>2.\u6253\u5305\u6587\u4ef6\u5939\u548c\u5220\u9664\u6587\u4ef6\u5939\uff0c\u4f7f\u7528shutil\u7684make_archive()\u548crmtree()<\/p>\n<pre class=\"\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">shutil.make_archive(source, \"zip\", source)\nshutil.rmtree(source)<\/code><\/pre>\n<ul>\n<li>\n<h1><strong>\u5b8c\u6574\u4ee3\u7801<\/strong><\/h1>\n<\/li>\n<\/ul>\n<p>\u8fd0\u884c\u6d4b\u8bd5\u65f6\uff0c\u8bf7\u4fee\u6539file\u7684\u8def\u5f84\u4e3a\u4f60\u81ea\u5df1\u7535\u8111\u91cc\u7684\u6587\u4ef6\u8def\u5f84<\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\"># -*- coding: UTF-8 -*-\nimport os\nimport shutil\ndef copy_and_zip(file_list, dst_folder_name):\n    '''\n    \u6279\u91cf\u590d\u5236\u6587\u4ef6\u5230\u6307\u5b9a\u6587\u4ef6\u5939\uff0c\u7136\u540e\u628a\u6307\u5b9a\u6587\u4ef6\u5939\u7684\u5185\u5bb9\u538b\u7f29\u6210ZIP\u5e76\u4e14\u5220\u6389\u8be5\u6587\u4ef6\u5939\n    :param file_list: \u6587\u4ef6\u6216\u6587\u4ef6\u5939\n    :param dst_folder_name: \u76ee\u6807\u538b\u7f29\u6587\u4ef6\u7684\u540d\u79f0\n    :return:\n    '''\n    for item in file_list:\n        copy_file(item, dst_folder_name)\n    # \u8fd9\u91cc\u6211\u628a\u8f93\u51fa\u6587\u4ef6\u7684\u8def\u5f84\u9009\u5230\u4e86\u7a0b\u5e8f\u6839\u76ee\u5f55\u4e0b\n    source = os.getcwd() + \"\\\\\" + dst_folder_name\n    shutil.make_archive(source, \"zip\", source)\n    shutil.rmtree(source)\ndef copy_file(srcfile, filename):\n    '''\n    \u628a\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u590d\u5236\u5230\u6307\u5b9a\u76ee\u5f55\u4e2d\n    :param srcfile: \u6587\u4ef6\u6216\u8005\u6587\u4ef6\u5939\u7684\u7edd\u5bf9\u8def\u5f84\n    :param filename: \u6307\u5b9a\u76ee\u5f55\n    :return:\n    '''\n    dstfile = os.path.abspath(os.getcwd())\n    # \u8fd9\u91cc\u6211\u628a\u8f93\u51fa\u6587\u4ef6\u7684\u8def\u5f84\u9009\u5230\u4e86\u7a0b\u5e8f\u6839\u76ee\u5f55\u4e0b\n    folder_name = dstfile + \"\\\\\" + filename + \"\\\\\"\n    if not os.path.isfile(srcfile):\n        last_name = os.path.basename(srcfile)\n        destination_name = folder_name + last_name\n        shutil.copytree(srcfile, destination_name)\n        print(\"copy %s -&gt; %s\" % (srcfile, destination_name))\n    else:\n        fpath, fname = os.path.split(folder_name)  # \u5206\u79bb\u6587\u4ef6\u540d\u548c\u8def\u5f84\n        if not os.path.exists(fpath):\n            os.makedirs(fpath)  # \u521b\u5efa\u8def\u5f84\n        shutil.copy2(srcfile, folder_name)  # \u79fb\u52a8\u6587\u4ef6\n        print(\"copy %s -&gt; %s\" % (srcfile, folder_name))\nif __name__ == '__main__':\n    file1 = \"C:\/Users\/Pictures\/1\/1.jpg\"\n    file2 = \"C:\/Users\/Pictures\/1\/sitemap.xml\"\n    file3 = \"C:\/lzw_programming\/resource\/\"\n    file_list = [file1, file2, file3]\n    # \u76ee\u6807\u538b\u7f29\u5305\u540d\n    folder_name = \"1234567\"\n    copy_and_zip(file_list, folder_name)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u660e\u786e\u9700\u6c42\uff08\u538b\u7f29\u6210ZIP\u6587\u4ef6\uff0c\u6587\u4ef6\u4e2d\u65e2\u6709\u666e\u901a\u6587\u4ef6\uff0c\u53c8\u6709\u6587\u4ef6\u5939\uff09 1.\u901a\u8fc7\u67d0\u79cd\u65b9\u5f0f\u83b7\u5f97\u4e00\u4e2a\u6587\u4ef6(\u6587\u4ef6\u5939)\u5217\u8868\u4f5c\u4e3a\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-228","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=228"}],"version-history":[{"count":2,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":1075,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/228\/revisions\/1075"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}