{"id":1186823,"date":"2025-01-15T19:57:29","date_gmt":"2025-01-15T11:57:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1186823.html"},"modified":"2025-01-15T19:57:32","modified_gmt":"2025-01-15T11:57:32","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5%e5%8e%8b%e7%bc%a9%e5%8c%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1186823.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165\u538b\u7f29\u5305"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25135529\/045821d2-8a5a-4568-a4ea-2c040ae6607a.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165\u538b\u7f29\u5305\" \/><\/p>\n<p><p> <strong>Python\u5bfc\u5165\u538b\u7f29\u5305\u7684\u65b9\u5f0f\u5305\u62ec\u4f7f\u7528\u6807\u51c6\u5e93\u6a21\u5757\u3001\u7b2c\u4e09\u65b9\u5e93\u5982<code>zipfile<\/code>\u3001<code>tarfile<\/code>\u3001<code>gzip<\/code>\u3001<code>bz2<\/code>\u3001<code>shutil<\/code>\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c<code>zipfile<\/code>\u6a21\u5757\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u4e4b\u4e00\u3002\u4e3a\u4e86\u8be6\u7ec6\u63cf\u8ff0\uff0c\u6211\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>zipfile<\/code>\u6a21\u5757\u6765\u5bfc\u5165\u548c\u5904\u7406\u538b\u7f29\u5305\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>zipfile<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>zipfile<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u4e13\u95e8\u7528\u4e8e\u5904\u7406ZIP\u6587\u4ef6\u7684\u6a21\u5757\u3002\u5b83\u5141\u8bb8\u4f60\u521b\u5efa\u3001\u8bfb\u53d6\u3001\u5199\u5165\u548c\u63d0\u53d6ZIP\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6\u538b\u7f29\u5305<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u8981\u5bfc\u5165<code>zipfile<\/code>\u6a21\u5757\uff0c\u7136\u540e\u6253\u5f00\u4e00\u4e2a\u538b\u7f29\u5305\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<h2><strong>\u6253\u5f00\u538b\u7f29\u5305<\/strong><\/h2>\n<p>zip_path = &#39;example.zip&#39;<\/p>\n<p>with zipfile.ZipFile(zip_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>    # \u5217\u51fa\u538b\u7f29\u5305\u4e2d\u7684\u6240\u6709\u6587\u4ef6<\/p>\n<p>    zip_ref.printdir()<\/p>\n<p>    # \u63d0\u53d6\u6240\u6709\u6587\u4ef6\u5230\u5f53\u524d\u76ee\u5f55<\/p>\n<p>    zip_ref.extractall()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>zipfile.ZipFile<\/code>\u6253\u5f00\u4e86\u4e00\u4e2a\u540d\u4e3a<code>example.zip<\/code>\u7684\u538b\u7f29\u5305\u3002<code>printdir()<\/code>\u65b9\u6cd5\u5217\u51fa\u4e86\u538b\u7f29\u5305\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff0c\u800c<code>extractall()<\/code>\u65b9\u6cd5\u5c06\u6240\u6709\u6587\u4ef6\u63d0\u53d6\u5230\u5f53\u524d\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h4>2. \u8bfb\u53d6\u5355\u4e2a\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u53ea\u9700\u8981\u8bfb\u53d6\u538b\u7f29\u5305\u4e2d\u7684\u67d0\u4e2a\u7279\u5b9a\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ZipFile.read()<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<h2><strong>\u6253\u5f00\u538b\u7f29\u5305<\/strong><\/h2>\n<p>zip_path = &#39;example.zip&#39;<\/p>\n<p>with zipfile.ZipFile(zip_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>    # \u8bfb\u53d6\u7279\u5b9a\u6587\u4ef6\u7684\u5185\u5bb9<\/p>\n<p>    file_content = zip_ref.read(&#39;specific_file.txt&#39;)<\/p>\n<p>    print(file_content.decode(&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bfb\u53d6\u4e86\u538b\u7f29\u5305\u4e2d\u540d\u4e3a<code>specific_file.txt<\/code>\u7684\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u89e3\u7801\u4e3aUTF-8\u5b57\u7b26\u4e32\u540e\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u538b\u7f29\u5305<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u8bfb\u53d6\u538b\u7f29\u5305\uff0c<code>zipfile<\/code>\u6a21\u5757\u8fd8\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u65b0\u7684\u538b\u7f29\u5305\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa\u538b\u7f29\u5305\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u7684\u538b\u7f29\u5305<\/strong><\/h2>\n<p>zip_path = &#39;new_archive.zip&#39;<\/p>\n<p>with zipfile.ZipFile(zip_path, &#39;w&#39;) as zip_ref:<\/p>\n<p>    # \u6dfb\u52a0\u6587\u4ef6\u5230\u538b\u7f29\u5305<\/p>\n<p>    zip_ref.write(&#39;file1.txt&#39;)<\/p>\n<p>    zip_ref.write(&#39;file2.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>new_archive.zip<\/code>\u7684\u538b\u7f29\u5305\uff0c\u5e76\u5c06<code>file1.txt<\/code>\u548c<code>file2.txt<\/code>\u4e24\u4e2a\u6587\u4ef6\u6dfb\u52a0\u5230\u4e86\u538b\u7f29\u5305\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>tarfile<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>tarfile<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7528\u4e8e\u5904\u7406TAR\u6587\u4ef6\u7684\u6a21\u5757\u3002\u5b83\u652f\u6301\u591a\u79cd\u538b\u7f29\u683c\u5f0f\uff0c\u5982gzip\u3001bz2\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6\u538b\u7f29\u5305<\/h4>\n<\/p>\n<p><p>\u4e0e<code>zipfile<\/code>\u6a21\u5757\u7c7b\u4f3c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>tarfile<\/code>\u6a21\u5757\u6765\u8bfb\u53d6TAR\u538b\u7f29\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tarfile<\/p>\n<h2><strong>\u6253\u5f00TAR\u538b\u7f29\u5305<\/strong><\/h2>\n<p>tar_path = &#39;example.tar.gz&#39;<\/p>\n<p>with tarfile.open(tar_path, &#39;r:gz&#39;) as tar_ref:<\/p>\n<p>    # \u5217\u51fa\u538b\u7f29\u5305\u4e2d\u7684\u6240\u6709\u6587\u4ef6<\/p>\n<p>    tar_ref.list()<\/p>\n<p>    # \u63d0\u53d6\u6240\u6709\u6587\u4ef6\u5230\u5f53\u524d\u76ee\u5f55<\/p>\n<p>    tar_ref.extractall()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>tarfile.open<\/code>\u6253\u5f00\u4e86\u4e00\u4e2a\u540d\u4e3a<code>example.tar.gz<\/code>\u7684\u538b\u7f29\u5305\uff0c\u5e76\u5217\u51fa\u4e86\u5176\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff0c\u7136\u540e\u5c06\u6240\u6709\u6587\u4ef6\u63d0\u53d6\u5230\u5f53\u524d\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h4>2. \u8bfb\u53d6\u5355\u4e2a\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u8bfb\u53d6TAR\u538b\u7f29\u5305\u4e2d\u7684\u67d0\u4e2a\u7279\u5b9a\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tarfile<\/p>\n<h2><strong>\u6253\u5f00TAR\u538b\u7f29\u5305<\/strong><\/h2>\n<p>tar_path = &#39;example.tar.gz&#39;<\/p>\n<p>with tarfile.open(tar_path, &#39;r:gz&#39;) as tar_ref:<\/p>\n<p>    # \u8bfb\u53d6\u7279\u5b9a\u6587\u4ef6\u7684\u5185\u5bb9<\/p>\n<p>    file_content = tar_ref.extractfile(&#39;specific_file.txt&#39;).read()<\/p>\n<p>    print(file_content.decode(&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bfb\u53d6\u4e86\u538b\u7f29\u5305\u4e2d\u540d\u4e3a<code>specific_file.txt<\/code>\u7684\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u89e3\u7801\u4e3aUTF-8\u5b57\u7b26\u4e32\u540e\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>shutil<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>shutil<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u9ad8\u7ea7\u6587\u4ef6\u64cd\u4f5c\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u590d\u5236\u3001\u79fb\u52a8\u3001\u5220\u9664\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u529f\u80fd\u3002\u5b83\u4e5f\u652f\u6301\u521b\u5efa\u548c\u89e3\u538b\u7f29ZIP\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1. \u89e3\u538b\u7f29ZIP\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>shutil<\/code>\u6a21\u5757\u89e3\u538b\u7f29ZIP\u6587\u4ef6\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<h2><strong>\u89e3\u538b\u7f29ZIP\u6587\u4ef6\u5230\u6307\u5b9a\u76ee\u5f55<\/strong><\/h2>\n<p>zip_path = &#39;example.zip&#39;<\/p>\n<p>extract_dir = &#39;extracted_files&#39;<\/p>\n<p>shutil.unpack_archive(zip_path, extract_dir)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>shutil.unpack_archive<\/code>\u5c06\u540d\u4e3a<code>example.zip<\/code>\u7684\u538b\u7f29\u5305\u89e3\u538b\u7f29\u5230<code>extracted_files<\/code>\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2. \u521b\u5efaZIP\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>shutil<\/code>\u6a21\u5757\u521b\u5efaZIP\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<h2><strong>\u521b\u5efaZIP\u6587\u4ef6<\/strong><\/h2>\n<p>source_dir = &#39;files_to_zip&#39;<\/p>\n<p>zip_path = &#39;new_archive.zip&#39;<\/p>\n<p>shutil.make_archive(zip_path.replace(&#39;.zip&#39;, &#39;&#39;), &#39;zip&#39;, source_dir)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>shutil.make_archive<\/code>\u5c06\u76ee\u5f55<code>files_to_zip<\/code>\u4e2d\u7684\u6587\u4ef6\u538b\u7f29\u5230<code>new_archive.zip<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>gzip<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>gzip<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7528\u4e8e\u5904\u7406GZIP\u6587\u4ef6\u7684\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u538b\u7f29\u548c\u89e3\u538b\u7f29\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u89e3\u538b\u7f29GZIP\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u89e3\u538b\u7f29GZIP\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gzip<\/p>\n<h2><strong>\u89e3\u538b\u7f29GZIP\u6587\u4ef6<\/strong><\/h2>\n<p>gzip_path = &#39;example.gz&#39;<\/p>\n<p>output_path = &#39;example.txt&#39;<\/p>\n<p>with gzip.open(gzip_path, &#39;rb&#39;) as gzip_ref:<\/p>\n<p>    with open(output_path, &#39;wb&#39;) as out_file:<\/p>\n<p>        out_file.write(gzip_ref.read())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>gzip.open<\/code>\u6253\u5f00\u4e86\u4e00\u4e2a\u540d\u4e3a<code>example.gz<\/code>\u7684GZIP\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u5199\u5165\u5230<code>example.txt<\/code>\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2. \u521b\u5efaGZIP\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>gzip<\/code>\u6a21\u5757\u521b\u5efaGZIP\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gzip<\/p>\n<h2><strong>\u521b\u5efaGZIP\u6587\u4ef6<\/strong><\/h2>\n<p>input_path = &#39;example.txt&#39;<\/p>\n<p>gzip_path = &#39;example.gz&#39;<\/p>\n<p>with open(input_path, &#39;rb&#39;) as in_file:<\/p>\n<p>    with gzip.open(gzip_path, &#39;wb&#39;) as gzip_ref:<\/p>\n<p>        gzip_ref.write(in_file.read())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>gzip.open<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>example.gz<\/code>\u7684GZIP\u6587\u4ef6\uff0c\u5e76\u5c06<code>example.txt<\/code>\u6587\u4ef6\u7684\u5185\u5bb9\u5199\u5165\u5230\u5176\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528<code>bz2<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>bz2<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7528\u4e8e\u5904\u7406BZIP2\u6587\u4ef6\u7684\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u538b\u7f29\u548c\u89e3\u538b\u7f29\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u89e3\u538b\u7f29BZIP2\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u89e3\u538b\u7f29BZIP2\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import bz2<\/p>\n<h2><strong>\u89e3\u538b\u7f29BZIP2\u6587\u4ef6<\/strong><\/h2>\n<p>bz2_path = &#39;example.bz2&#39;<\/p>\n<p>output_path = &#39;example.txt&#39;<\/p>\n<p>with bz2.open(bz2_path, &#39;rb&#39;) as bz2_ref:<\/p>\n<p>    with open(output_path, &#39;wb&#39;) as out_file:<\/p>\n<p>        out_file.write(bz2_ref.read())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>bz2.open<\/code>\u6253\u5f00\u4e86\u4e00\u4e2a\u540d\u4e3a<code>example.bz2<\/code>\u7684BZIP2\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u5199\u5165\u5230<code>example.txt<\/code>\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2. \u521b\u5efaBZIP2\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>bz2<\/code>\u6a21\u5757\u521b\u5efaBZIP2\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import bz2<\/p>\n<h2><strong>\u521b\u5efaBZIP2\u6587\u4ef6<\/strong><\/h2>\n<p>input_path = &#39;example.txt&#39;<\/p>\n<p>bz2_path = &#39;example.bz2&#39;<\/p>\n<p>with open(input_path, &#39;rb&#39;) as in_file:<\/p>\n<p>    with bz2.open(bz2_path, &#39;wb&#39;) as bz2_ref:<\/p>\n<p>        bz2_ref.write(in_file.read())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>bz2.open<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>example.bz2<\/code>\u7684BZIP2\u6587\u4ef6\uff0c\u5e76\u5c06<code>example.txt<\/code>\u6587\u4ef6\u7684\u5185\u5bb9\u5199\u5165\u5230\u5176\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u7684\u6807\u51c6\u5e93\u6a21\u5757<code>zipfile<\/code>\u3001<code>tarfile<\/code>\u3001<code>shutil<\/code>\u3001<code>gzip<\/code>\u3001<code>bz2<\/code>\u7b49\u6765\u5bfc\u5165\u548c\u5904\u7406\u538b\u7f29\u5305\u3002\u8fd9\u4e9b\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u6ee1\u8db3\u6211\u4eec\u5728\u5904\u7406\u538b\u7f29\u5305\u65f6\u7684\u5404\u79cd\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981\u5904\u7406ZIP\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>zipfile<\/code>\u6a21\u5757\uff1b\u5982\u679c\u9700\u8981\u5904\u7406TAR\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>tarfile<\/code>\u6a21\u5757\uff1b\u5982\u679c\u9700\u8981\u7b80\u5355\u7684\u538b\u7f29\u548c\u89e3\u538b\u7f29\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shutil<\/code>\u3001<code>gzip<\/code>\u6216<code>bz2<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u4f60\u5bf9Python\u5982\u4f55\u5bfc\u5165\u538b\u7f29\u5305\u6709\u4e86\u66f4\u6df1\u5165\u7684\u4e86\u89e3\uff0c\u5e76\u80fd\u591f\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u77e5\u8bc6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u89e3\u538b\u7f29\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>zipfile<\/code>\u6a21\u5757\u6765\u89e3\u538b\u7f29ZIP\u6587\u4ef6\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>zipfile.ZipFile<\/code>\u7c7b\u6253\u5f00\u538b\u7f29\u6587\u4ef6\uff0c\u5e76\u8c03\u7528<code>extractall()<\/code>\u65b9\u6cd5\u6765\u89e3\u538b\u7f29\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import zipfile\n\nwith zipfile.ZipFile(&#39;example.zip&#39;, &#39;r&#39;) as zip_ref:\n    zip_ref.extractall(&#39;destination_directory&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5c06<code>example.zip<\/code>\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u89e3\u538b\u5230\u6307\u5b9a\u7684\u76ee\u5f55\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bfb\u53d6\u538b\u7f29\u5305\u5185\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>zipfile<\/code>\u6a21\u5757\u6253\u5f00\u538b\u7f29\u6587\u4ef6\u5e76\u8bfb\u53d6\u5176\u4e2d\u7684\u6587\u4ef6\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u8bfb\u53d6\u538b\u7f29\u5305\u5185\u7684\u7279\u5b9a\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-python\">import zipfile\n\nwith zipfile.ZipFile(&#39;example.zip&#39;, &#39;r&#39;) as zip_ref:\n    with zip_ref.open(&#39;file_inside_zip.txt&#39;) as file:\n        content = file.read()\n        print(content.decode(&#39;utf-8&#39;))\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u5e2e\u52a9\u60a8\u76f4\u63a5\u8bfb\u53d6\u538b\u7f29\u5305\u5185\u7684\u6587\u4ef6\uff0c\u800c\u65e0\u9700\u5c06\u5176\u89e3\u538b\u5230\u786c\u76d8\u4e0a\u3002<\/p>\n<p><strong>Python\u652f\u6301\u54ea\u4e9b\u7c7b\u578b\u7684\u538b\u7f29\u6587\u4ef6\uff1f<\/strong><br \/>Python\u7684\u6807\u51c6\u5e93\u4e3b\u8981\u652f\u6301ZIP\u683c\u5f0f\u7684\u538b\u7f29\u6587\u4ef6\u3002\u4e0d\u8fc7\uff0c\u60a8\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u6765\u5904\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u538b\u7f29\u6587\u4ef6\uff0c\u4f8b\u5982<code>tarfile<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u5904\u7406TAR\u3001GZ\u548cBZ2\u683c\u5f0f\u7684\u538b\u7f29\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406TAR\u6587\u4ef6\u7684\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import tarfile\n\nwith tarfile.open(&#39;example.tar.gz&#39;, &#39;r:gz&#39;) as tar_ref:\n    tar_ref.extractall(&#39;destination_directory&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u89e3\u538b\u7f29\u4e00\u4e2aGZ\u538b\u7f29\u7684TAR\u6587\u4ef6\u3002\u5bf9\u4e8e\u4e0d\u540c\u683c\u5f0f\u7684\u538b\u7f29\u6587\u4ef6\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u76f8\u5e94\u7684\u6a21\u5757\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5bfc\u5165\u538b\u7f29\u5305\u7684\u65b9\u5f0f\u5305\u62ec\u4f7f\u7528\u6807\u51c6\u5e93\u6a21\u5757\u3001\u7b2c\u4e09\u65b9\u5e93\u5982zipfile\u3001tarfile\u3001gzip\u3001bz2\u3001s [&hellip;]","protected":false},"author":3,"featured_media":1186825,"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\/1186823"}],"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=1186823"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186823\/revisions"}],"predecessor-version":[{"id":1186827,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186823\/revisions\/1186827"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1186825"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1186823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1186823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1186823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}