{"id":1047000,"date":"2024-12-31T13:38:40","date_gmt":"2024-12-31T05:38:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1047000.html"},"modified":"2024-12-31T13:38:43","modified_gmt":"2024-12-31T05:38:43","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%8e%8b%e7%bc%a9%e6%89%93%e5%8c%85%e4%b8%batgz%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1047000.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u538b\u7f29\u6253\u5305\u4e3atgz\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/e8152dcc-3145-492f-9774-a0781802976f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u538b\u7f29\u6253\u5305\u4e3atgz\u683c\u5f0f\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>tarfile<\/code>\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6587\u4ef6\u538b\u7f29\u6253\u5305\u4e3atgz\u683c\u5f0f\u3002\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165tarfile\u6a21\u5757\u3001\u4f7f\u7528open\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u538b\u7f29\u5305\u3001\u4f7f\u7528add\u65b9\u6cd5\u6dfb\u52a0\u6587\u4ef6\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5229\u7528Python\u5b9e\u73b0\u6587\u4ef6\u538b\u7f29\u6253\u5305\u4e3atgz\u683c\u5f0f\uff0c\u5e76\u89e3\u6790\u6bcf\u4e2a\u6b65\u9aa4\u7684\u5177\u4f53\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5bfc\u5165tarfile\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165Python\u6807\u51c6\u5e93\u4e2d\u7684<code>tarfile<\/code>\u6a21\u5757\u3002<code>tarfile<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5de5\u5177\u7528\u4e8e\u521b\u5efa\u3001\u8bfb\u5199\u3001\u89e3\u538b\u7f29tar\u6587\u4ef6\uff0c\u5305\u62ec\u5e38\u89c1\u7684<code>.tar<\/code>, <code>.tar.gz<\/code>, <code>.tar.bz2<\/code>\u7b49\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tarfile<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u521b\u5efa\u4e00\u4e2a\u538b\u7f29\u5305<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528<code>tarfile.open<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u538b\u7f29\u5305\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u4e3b\u8981\u53c2\u6570\uff1a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\u3002\u6a21\u5f0f<code>w:gz<\/code>\u8868\u793a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684gzip\u538b\u7f29\u5305\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with tarfile.open(&quot;example.tgz&quot;, &quot;w:gz&quot;) as tar:<\/p>\n<p>    # \u8fd9\u91cc\u5c06\u6dfb\u52a0\u6587\u4ef6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u6dfb\u52a0\u6587\u4ef6\u5230\u538b\u7f29\u5305<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>tarfile<\/code>\u5bf9\u8c61\u7684<code>add<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u6587\u4ef6\u6216\u76ee\u5f55\u6dfb\u52a0\u5230\u538b\u7f29\u5305\u4e2d\u3002<code>add<\/code>\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u8981\u6dfb\u52a0\u7684\u6587\u4ef6\u8def\u5f84\u548c\u53ef\u9009\u7684\u5728\u538b\u7f29\u5305\u4e2d\u7684\u5b58\u50a8\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with tarfile.open(&quot;example.tgz&quot;, &quot;w:gz&quot;) as tar:<\/p>\n<p>    tar.add(&quot;file1.txt&quot;)<\/p>\n<p>    tar.add(&quot;directory\/&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u591a\u4e2a\u6587\u4ef6\u548c\u76ee\u5f55\u6253\u5305\u6210\u4e00\u4e2atgz\u683c\u5f0f\u7684\u538b\u7f29\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tarfile<\/p>\n<p>import os<\/p>\n<p>def create_tgz_archive(output_filename, source_dir):<\/p>\n<p>    with tarfile.open(output_filename, &quot;w:gz&quot;) as tar:<\/p>\n<p>        tar.add(source_dir, arcname=os.path.basename(source_dir))<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u521b\u5efa\u538b\u7f29\u5305<\/strong><\/h2>\n<p>create_tgz_archive(&quot;output_archive.tgz&quot;, &quot;source_directory&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u9010\u6b65\u89e3\u6790<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u6a21\u5757<\/strong>\uff1a\u9996\u5148\u5bfc\u5165<code>tarfile<\/code>\u548c<code>os<\/code>\u6a21\u5757\u3002<\/li>\n<li><strong>\u5b9a\u4e49\u51fd\u6570<\/strong>\uff1a\u5b9a\u4e49<code>create_tgz_archive<\/code>\u51fd\u6570\uff0c\u63a5\u53d7\u8f93\u51fa\u6587\u4ef6\u540d\u548c\u6e90\u76ee\u5f55\u4f5c\u4e3a\u53c2\u6570\u3002<\/li>\n<li><strong>\u521b\u5efa\u538b\u7f29\u5305<\/strong>\uff1a\u4f7f\u7528<code>tarfile.open<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684gzip\u538b\u7f29\u5305\u3002<\/li>\n<li><strong>\u6dfb\u52a0\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528<code>tar.add<\/code>\u65b9\u6cd5\u5c06\u6e90\u76ee\u5f55\u53ca\u5176\u5185\u5bb9\u6dfb\u52a0\u5230\u538b\u7f29\u5305\u4e2d\u3002<\/li>\n<li><strong>\u8c03\u7528\u51fd\u6570<\/strong>\uff1a\u8c03\u7528<code>create_tgz_archive<\/code>\u51fd\u6570\uff0c\u4f20\u5165\u8f93\u51fa\u6587\u4ef6\u540d\u548c\u8981\u538b\u7f29\u7684\u76ee\u5f55\u3002<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u5904\u7406\u5927\u6587\u4ef6\u548c\u76ee\u5f55<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u548c\u76ee\u5f55\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u8003\u8651\u5185\u5b58\u548c\u6027\u80fd\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u6216\u5206\u5757\u5904\u7406\u7684\u65b9\u5f0f\u6765\u4f18\u5316\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u5927\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tarfile<\/p>\n<p>import os<\/p>\n<p>def create_tgz_archive(output_filename, source_dir):<\/p>\n<p>    with tarfile.open(output_filename, &quot;w:gz&quot;) as tar:<\/p>\n<p>        for root, dirs, files in os.walk(source_dir):<\/p>\n<p>            for file in files:<\/p>\n<p>                file_path = os.path.join(root, file)<\/p>\n<p>                tar.add(file_path, arcname=os.path.relpath(file_path, source_dir))<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u521b\u5efa\u538b\u7f29\u5305<\/strong><\/h2>\n<p>create_tgz_archive(&quot;large_output_archive.tgz&quot;, &quot;large_source_directory&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>os.walk<\/code>\u904d\u5386\u76ee\u5f55\u6811\uff0c\u9010\u4e00\u5c06\u6587\u4ef6\u6dfb\u52a0\u5230\u538b\u7f29\u5305\u4e2d\uff0c\u4ece\u800c\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u89e3\u538b\u7f29tgz\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u521b\u5efa\u538b\u7f29\u5305\uff0c<code>tarfile<\/code>\u6a21\u5757\u8fd8\u53ef\u4ee5\u7528\u4e8e\u89e3\u538b\u7f29tgz\u6587\u4ef6\u3002\u4f7f\u7528<code>tarfile.open<\/code>\u65b9\u6cd5\u4ee5\u8bfb\u53d6\u6a21\u5f0f\u6253\u5f00\u538b\u7f29\u5305\uff0c\u7136\u540e\u4f7f\u7528<code>extractall<\/code>\u65b9\u6cd5\u89e3\u538b\u6240\u6709\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tarfile<\/p>\n<p>def extract_tgz_archive(input_filename, output_dir):<\/p>\n<p>    with tarfile.open(input_filename, &quot;r:gz&quot;) as tar:<\/p>\n<p>        tar.extractall(path=output_dir)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u89e3\u538b\u7f29\u5305<\/strong><\/h2>\n<p>extract_tgz_archive(&quot;example.tgz&quot;, &quot;output_directory&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528Python\u7684<code>tarfile<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6587\u4ef6\u548c\u76ee\u5f55\u538b\u7f29\u6253\u5305\u4e3atgz\u683c\u5f0f\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4ece\u5bfc\u5165\u6a21\u5757\u5230\u521b\u5efa\u538b\u7f29\u5305\u548c\u89e3\u538b\u7f29\u5305\u7684\u5b8c\u6574\u8fc7\u7a0b\uff0c\u5e76\u63d0\u4f9b\u4e86\u5904\u7406\u5927\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u8fdb\u884c\u6587\u4ef6\u538b\u7f29\u548c\u89e3\u538b\u7f29\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efatgz\u683c\u5f0f\u7684\u538b\u7f29\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>tarfile<\/code>\u6a21\u5757\u6765\u521b\u5efatgz\u683c\u5f0f\u7684\u538b\u7f29\u6587\u4ef6\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>tarfile.open()<\/code>\u65b9\u6cd5\u4ee5\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>add()<\/code>\u65b9\u6cd5\u5c06\u8981\u538b\u7f29\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u6dfb\u52a0\u5230\u538b\u7f29\u5305\u4e2d\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u64cd\u4f5c\uff1a  <\/p>\n<pre><code class=\"language-python\">import tarfile\n\nwith tarfile.open(&#39;your_archive.tgz&#39;, &#39;w:gz&#39;) as tar:\n    tar.add(&#39;your_directory_or_file&#39;)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u6307\u5b9a\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u538b\u7f29\u4e3a<code>your_archive.tgz<\/code>\u6587\u4ef6\u3002<\/p>\n<p><strong>tgz\u6587\u4ef6\u4e0e\u5176\u4ed6\u538b\u7f29\u683c\u5f0f\u76f8\u6bd4\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>tgz\u6587\u4ef6\uff0c\u5b9e\u9645\u4e0a\u662ftar\u6587\u4ef6\u7ecf\u8fc7gzip\u538b\u7f29\u7684\u7ed3\u679c\uff0c\u5177\u6709\u591a\u79cd\u4f18\u52bf\u3002\u9996\u5148\uff0c\u5b83\u80fd\u591f\u5c06\u591a\u4e2a\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u6253\u5305\u6210\u4e00\u4e2a\u6587\u4ef6\uff0c\u65b9\u4fbf\u7ba1\u7406\u548c\u4f20\u8f93\u3002\u5176\u6b21\uff0ctgz\u683c\u5f0f\u901a\u5e38\u80fd\u63d0\u4f9b\u66f4\u9ad8\u7684\u538b\u7f29\u6bd4\uff0c\u8282\u7701\u5b58\u50a8\u7a7a\u95f4\u3002\u6b64\u5916\uff0ctgz\u6587\u4ef6\u517c\u5bb9\u6027\u597d\uff0c\u8bb8\u591a\u64cd\u4f5c\u7cfb\u7edf\u548c\u5de5\u5177\u90fd\u652f\u6301\u89e3\u538b\u548c\u521b\u5efatgz\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u89e3\u538btgz\u6587\u4ef6\uff1f<\/strong><br \/>\u89e3\u538btgz\u6587\u4ef6\u540c\u6837\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>tarfile<\/code>\u6a21\u5757\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>tarfile.open()<\/code>\u65b9\u6cd5\u4ee5\u8bfb\u53d6\u6a21\u5f0f\u6253\u5f00tgz\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>extractall()<\/code>\u65b9\u6cd5\u5c06\u5185\u5bb9\u89e3\u538b\u5230\u6307\u5b9a\u76ee\u5f55\u4e2d\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import tarfile\n\nwith tarfile.open(&#39;your_archive.tgz&#39;, &#39;r:gz&#39;) as tar:\n    tar.extractall(path=&#39;destination_directory&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u4f1a\u628a<code>your_archive.tgz<\/code>\u4e2d\u7684\u6240\u6709\u5185\u5bb9\u89e3\u538b\u5230<code>destination_directory<\/code>\u76ee\u5f55\u4e0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528tarfile\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6587\u4ef6\u538b\u7f29\u6253\u5305\u4e3atgz\u683c\u5f0f\u3002\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165tarfile [&hellip;]","protected":false},"author":3,"featured_media":1047008,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1047000"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1047000"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1047000\/revisions"}],"predecessor-version":[{"id":1047009,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1047000\/revisions\/1047009"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1047008"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1047000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1047000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1047000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}