{"id":933200,"date":"2024-12-26T18:11:59","date_gmt":"2024-12-26T10:11:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/933200.html"},"modified":"2024-12-26T18:12:01","modified_gmt":"2024-12-26T10:12:01","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%a7%a3%e5%8e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/933200.html","title":{"rendered":"\u5982\u4f55\u7528python\u89e3\u538b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070715\/027c2151-8a1d-4a85-a449-ffaa4550e4fb.webp\" alt=\"\u5982\u4f55\u7528python\u89e3\u538b\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u89e3\u538b\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684<code>zipfile<\/code>\u6a21\u5757\u3001\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684<code>tarfile<\/code>\u6a21\u5757\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u7684<code>patool<\/code>\u5e93\u3002<\/strong>\u5176\u4e2d\uff0c<code>zipfile<\/code>\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u5904\u7406ZIP\u6587\u4ef6\uff0c<code>tarfile<\/code>\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u5904\u7406TAR\u6587\u4ef6\uff0c\u800c<code>patool<\/code>\u5e93\u53ef\u4ee5\u5904\u7406\u591a\u79cd\u538b\u7f29\u683c\u5f0f\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u89e3\u538b\u6587\u4ef6\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u548c\u5b9e\u7528\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528ZIPFILE\u6a21\u5757\u89e3\u538bZIP\u6587\u4ef6<\/p>\n<\/p>\n<p><p><code>zipfile<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u89e3\u538bZIP\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>1. \u57fa\u7840\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>zipfile<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u63a5\u53e3\u7528\u4e8e\u5904\u7406ZIP\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u89e3\u538b\u4e00\u4e2aZIP\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>def extract_zip(file_path, extract_to):<\/p>\n<p>    with zipfile.ZipFile(file_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>        zip_ref.extractall(extract_to)<\/p>\n<p>extract_zip(&#39;example.zip&#39;, &#39;extracted_files&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>ZipFile<\/code>\u7c7b\u7528\u4e8e\u6253\u5f00ZIP\u6587\u4ef6\uff0c<code>extractall<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u6587\u4ef6\u89e3\u538b\u5230\u6307\u5b9a\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>2. \u5904\u7406\u5bc6\u7801\u4fdd\u62a4\u7684ZIP\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5982\u679cZIP\u6587\u4ef6\u662f\u52a0\u5bc6\u7684\uff0c\u4f60\u9700\u8981\u63d0\u4f9b\u5bc6\u7801\u624d\u80fd\u89e3\u538b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>def extract_zip_with_password(file_path, extract_to, password):<\/p>\n<p>    with zipfile.ZipFile(file_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>        zip_ref.extractall(extract_to, pwd=password.encode())<\/p>\n<p>extract_zip_with_password(&#39;example.zip&#39;, &#39;extracted_files&#39;, &#39;your_password&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6ce8\u610f\uff0c\u5bc6\u7801\u5fc5\u987b\u4ee5\u5b57\u8282\u5f62\u5f0f\u4f20\u9012\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>3. \u5904\u7406\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ZipFile<\/code>\u7c7b\u7684<code>infolist<\/code>\u65b9\u6cd5\u9010\u4e2a\u89e3\u538b\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>def extract_large_zip(file_path, extract_to):<\/p>\n<p>    with zipfile.ZipFile(file_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>        for member in zip_ref.infolist():<\/p>\n<p>            zip_ref.extract(member, extract_to)<\/p>\n<p>extract_large_zip(&#39;large_example.zip&#39;, &#39;extracted_files&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u5185\u5b58\u4f7f\u7528\uff0c\u5c24\u5176\u662f\u5728\u89e3\u538b\u5927\u578b\u6587\u4ef6\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528TARFILE\u6a21\u5757\u89e3\u538bTAR\u6587\u4ef6<\/p>\n<\/p>\n<p><p><code>tarfile<\/code>\u6a21\u5757\u4e5f\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u652f\u6301\u5904\u7406TAR\u3001TAR.GZ\u3001TAR.BZ2\u7b49\u683c\u5f0f\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>1. \u57fa\u7840\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u89e3\u538bTAR\u6587\u4ef6\u7684\u57fa\u672c\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tarfile<\/p>\n<p>def extract_tar(file_path, extract_to):<\/p>\n<p>    with tarfile.open(file_path, &#39;r&#39;) as tar_ref:<\/p>\n<p>        tar_ref.extractall(extract_to)<\/p>\n<p>extract_tar(&#39;example.tar&#39;, &#39;extracted_files&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0e<code>zipfile<\/code>\u6a21\u5757\u7c7b\u4f3c\uff0c<code>tarfile<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u63a5\u53e3\u7528\u4e8e\u89e3\u538b\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>2. \u89e3\u538bTAR.GZ\u548cTAR.BZ2\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u538b\u7f29\u7684TAR\u6587\u4ef6\uff08\u5982TAR.GZ\u548cTAR.BZ2\uff09\uff0c\u53ea\u9700\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u6a21\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tarfile<\/p>\n<p>def extract_tar_gz(file_path, extract_to):<\/p>\n<p>    with tarfile.open(file_path, &#39;r:gz&#39;) as tar_ref:<\/p>\n<p>        tar_ref.extractall(extract_to)<\/p>\n<p>def extract_tar_bz2(file_path, extract_to):<\/p>\n<p>    with tarfile.open(file_path, &#39;r:bz2&#39;) as tar_ref:<\/p>\n<p>        tar_ref.extractall(extract_to)<\/p>\n<p>extract_tar_gz(&#39;example.tar.gz&#39;, &#39;extracted_files&#39;)<\/p>\n<p>extract_tar_bz2(&#39;example.tar.bz2&#39;, &#39;extracted_files&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6a21\u5f0f<code>&#39;r:gz&#39;<\/code>\u548c<code>&#39;r:bz2&#39;<\/code>\u5206\u522b\u7528\u4e8e\u8bfb\u53d6TAR.GZ\u548cTAR.BZ2\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>3. \u5904\u7406\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u7c7b\u4f3c\u4e8e<code>zipfile<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u9010\u4e2a\u63d0\u53d6\u6587\u4ef6\u4ee5\u5904\u7406\u5927\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tarfile<\/p>\n<p>def extract_large_tar(file_path, extract_to):<\/p>\n<p>    with tarfile.open(file_path, &#39;r&#39;) as tar_ref:<\/p>\n<p>        for member in tar_ref.getmembers():<\/p>\n<p>            tar_ref.extract(member, extract_to)<\/p>\n<p>extract_large_tar(&#39;large_example.tar&#39;, &#39;extracted_files&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9010\u4e2a\u63d0\u53d6\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528PATOOL\u5e93\u89e3\u538b\u591a\u79cd\u683c\u5f0f\u6587\u4ef6<\/p>\n<\/p>\n<p><p><code>patool<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u652f\u6301\u591a\u79cd\u538b\u7f29\u683c\u5f0f\u3002\u9700\u8981\u5148\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install patool<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1. \u57fa\u7840\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>patool<\/code>\u89e3\u538b\u6587\u4ef6\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import patoolib<\/p>\n<p>def extract_with_patool(file_path, extract_to):<\/p>\n<p>    patoolib.extract_archive(file_path, outdir=extract_to)<\/p>\n<p>extract_with_patool(&#39;example.rar&#39;, &#39;extracted_files&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>patoolib.extract_archive<\/code>\u65b9\u6cd5\u53ef\u4ee5\u81ea\u52a8\u8bc6\u522b\u6587\u4ef6\u683c\u5f0f\u5e76\u8fdb\u884c\u89e3\u538b\u3002<\/p>\n<\/p>\n<p><h3>2. \u652f\u6301\u7684\u683c\u5f0f<\/h3>\n<\/p>\n<p><p><code>patool<\/code>\u652f\u6301\u591a\u79cd\u683c\u5f0f\uff0c\u5305\u62ecZIP\u3001TAR\u3001RAR\u30017Z\u7b49\uff0c\u5177\u4f53\u652f\u6301\u7684\u683c\u5f0f\u53ef\u4ee5\u901a\u8fc7<code>patoolib.get_supported_formats()<\/code>\u67e5\u770b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import patoolib<\/p>\n<p>supported_formats = patoolib.get_supported_formats()<\/p>\n<p>print(supported_formats)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4f7f\u7528\u5916\u90e8\u5de5\u5177<\/h3>\n<\/p>\n<p><p><code>patool<\/code>\u4f9d\u8d56\u4e8e\u7cfb\u7edf\u4e0a\u7684\u89e3\u538b\u5de5\u5177\uff0c\u5982<code>unrar<\/code>\u3001<code>7z<\/code>\u7b49\uff0c\u786e\u4fdd\u8fd9\u4e9b\u5de5\u5177\u5df2\u5b89\u88c5\u5e76\u5728\u73af\u5883\u8def\u5f84\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u89e3\u538b\u9519\u8bef\u548c\u5f02\u5e38<\/p>\n<\/p>\n<p><p>\u5728\u89e3\u538b\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\u548c\u5f02\u5e38\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>1. \u5f02\u5e38\u6355\u83b7<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>try-except<\/code>\u5757\u6355\u83b7\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u9519\u8bef\u800c\u5d29\u6e83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_extract_zip(file_path, extract_to):<\/p>\n<p>    try:<\/p>\n<p>        with zipfile.ZipFile(file_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>            zip_ref.extractall(extract_to)<\/p>\n<p>    except zipfile.BadZipFile:<\/p>\n<p>        print(&quot;Error: Bad ZIP file&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>safe_extract_zip(&#39;corrupted.zip&#39;, &#39;extracted_files&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7\u6709\u52a9\u4e8e\u8c03\u8bd5\u548c\u5206\u6790\u95ee\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>import zipfile<\/p>\n<p>logging.basicConfig(filename=&#39;extract_errors.log&#39;, level=logging.ERROR)<\/p>\n<p>def log_and_extract_zip(file_path, extract_to):<\/p>\n<p>    try:<\/p>\n<p>        with zipfile.ZipFile(file_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>            zip_ref.extractall(extract_to)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        logging.error(f&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to extract {file_path}: {e}&quot;)<\/p>\n<p>log_and_extract_zip(&#39;example.zip&#39;, &#39;extracted_files&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u6821\u9a8c\u6587\u4ef6\u5b8c\u6574\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u89e3\u538b\u524d\uff0c\u53ef\u4ee5\u5148\u6821\u9a8c\u6587\u4ef6\u7684\u5b8c\u6574\u6027\uff0c\u4ee5\u786e\u4fdd\u6587\u4ef6\u672a\u635f\u574f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>def check_zip_integrity(file_path):<\/p>\n<p>    try:<\/p>\n<p>        with zipfile.ZipFile(file_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>            corrupt_file = zip_ref.testzip()<\/p>\n<p>            if corrupt_file is not None:<\/p>\n<p>                print(f&quot;Corrupt file found: {corrupt_file}&quot;)<\/p>\n<p>            else:<\/p>\n<p>                print(&quot;ZIP file is intact&quot;)<\/p>\n<p>    except zipfile.BadZipFile:<\/p>\n<p>        print(&quot;Error: Bad ZIP file&quot;)<\/p>\n<p>check_zip_integrity(&#39;example.zip&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528Python\u7684<code>zipfile<\/code>\u3001<code>tarfile<\/code>\u6a21\u5757\u548c<code>patool<\/code>\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u89e3\u538b\u5404\u79cd\u683c\u5f0f\u7684\u6587\u4ef6\u3002\u6839\u636e\u6587\u4ef6\u683c\u5f0f\u7684\u4e0d\u540c\u9009\u62e9\u5408\u9002\u7684\u6a21\u5757\u548c\u65b9\u6cd5\uff0c\u5e76\u6839\u636e\u9700\u8981\u5904\u7406\u5927\u6587\u4ef6\u3001\u5bc6\u7801\u4fdd\u62a4\u6587\u4ef6\u4ee5\u53ca\u5f02\u5e38\u60c5\u51b5\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u63d0\u9ad8\u6587\u4ef6\u5904\u7406\u7684\u6548\u7387\u548c\u7a33\u5b9a\u6027\uff0c\u7279\u522b\u662f\u5728\u81ea\u52a8\u5316\u811a\u672c\u548c\u6570\u636e\u5904\u7406\u7ba1\u9053\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u89e3\u538bZIP\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u89e3\u538bZIP\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>zipfile<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>zipfile.ZipFile<\/code>\u7c7b\u6253\u5f00ZIP\u6587\u4ef6\u3002\u63a5\u4e0b\u6765\uff0c\u8c03\u7528<code>extractall()<\/code>\u65b9\u6cd5\u6765\u89e3\u538b\u6240\u6709\u6587\u4ef6\uff0c\u6216\u4f7f\u7528<code>extract()<\/code>\u65b9\u6cd5\u6765\u89e3\u538b\u7279\u5b9a\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import zipfile\n\nwith zipfile.ZipFile(&#39;yourfile.zip&#39;, &#39;r&#39;) as zip_ref:\n    zip_ref.extractall(&#39;target_directory&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5c06<code>yourfile.zip<\/code>\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u89e3\u538b\u5230\u6307\u5b9a\u7684<code>target_directory<\/code>\u4e2d\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u53ef\u4ee5\u89e3\u538b\u5176\u4ed6\u683c\u5f0f\u7684\u538b\u7f29\u6587\u4ef6\uff1f<\/strong><br \/>\u9664\u4e86ZIP\u683c\u5f0f\uff0cPython\u8fd8\u652f\u6301\u591a\u79cd\u5176\u4ed6\u538b\u7f29\u683c\u5f0f\u7684\u89e3\u538b\uff0c\u6bd4\u5982TAR\u3001GZ\u3001BZ2\u7b49\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>tarfile<\/code>\u6a21\u5757\u6765\u5904\u7406TAR\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u89e3\u538bTAR\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import tarfile\n\nwith tarfile.open(&#39;yourfile.tar&#39;, &#39;r&#39;) as tar_ref:\n    tar_ref.extractall(&#39;target_directory&#39;)\n<\/code><\/pre>\n<p>\u5bf9\u4e8eGZ\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u5c06<code>tarfile.open()<\/code>\u4e2d\u7684\u6a21\u5f0f\u66f4\u6539\u4e3a<code>&#39;r:gz&#39;<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u89e3\u538b\u8fc7\u7a0b\u4e2d\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u89e3\u538b\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u9519\u8bef\uff0c\u6bd4\u5982\u6587\u4ef6\u635f\u574f\u6216\u8def\u5f84\u95ee\u9898\u3002\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import zipfile\n\ntry:\n    with zipfile.ZipFile(&#39;yourfile.zip&#39;, &#39;r&#39;) as zip_ref:\n        zip_ref.extractall(&#39;target_directory&#39;)\nexcept zipfile.BadZipFile:\n    print(&quot;ZIP\u6587\u4ef6\u635f\u574f\u6216\u4e0d\u662f\u6709\u6548\u7684ZIP\u6587\u4ef6\u3002&quot;)\nexcept FileNotFoundError:\n    print(&quot;\u6307\u5b9a\u7684\u6587\u4ef6\u672a\u627e\u5230\u3002&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u89e3\u538b\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684zipfile\u6a21\u5757\u3001\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684tarfile\u6a21\u5757\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u7684 [&hellip;]","protected":false},"author":3,"featured_media":933203,"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\/933200"}],"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=933200"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933200\/revisions"}],"predecessor-version":[{"id":933207,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933200\/revisions\/933207"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/933203"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=933200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=933200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=933200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}