{"id":1168121,"date":"2025-01-15T15:54:09","date_gmt":"2025-01-15T07:54:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1168121.html"},"modified":"2025-01-15T15:54:13","modified_gmt":"2025-01-15T07:54:13","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87python%e8%a7%a3%e5%8e%8b%e6%96%87%e4%bb%b6%e5%8c%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1168121.html","title":{"rendered":"\u5982\u4f55\u901a\u8fc7python\u89e3\u538b\u6587\u4ef6\u5305"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26065443\/177dd053-77f9-4128-a674-62e0f84d958f.webp\" alt=\"\u5982\u4f55\u901a\u8fc7python\u89e3\u538b\u6587\u4ef6\u5305\" \/><\/p>\n<p><p> <strong>\u901a\u8fc7Python\u89e3\u538b\u6587\u4ef6\u5305\u7684\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6807\u51c6\u5e93zipfile\u3001\u4f7f\u7528\u6807\u51c6\u5e93tarfile\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93shutil\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93patool\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u6807\u51c6\u5e93zipfile<\/strong> \u662f\u6700\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528zipfile\u5e93\u6765\u89e3\u538b\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u6807\u51c6\u5e93zipfile<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528zipfile\u5e93\u89e3\u538b\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u4e14\u65b9\u4fbf\u3002zipfile\u5e93\u662fPython\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165\u5e76\u4f7f\u7528\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>def extract_zip_file(zip_file_path, extract_to):<\/p>\n<p>    with zipfile.ZipFile(zip_file_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>        zip_ref.extractall(extract_to)<\/p>\n<p>    print(f&quot;Extracted all contents of {zip_file_path} to {extract_to}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>extract_zip_file(&#39;example.zip&#39;, &#39;extracted_files&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>extract_zip_file<\/code> \u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1azip\u6587\u4ef6\u7684\u8def\u5f84\u548c\u89e3\u538b\u6587\u4ef6\u7684\u76ee\u6807\u76ee\u5f55\u3002\u4f7f\u7528 <code>zipfile.ZipFile<\/code> \u6253\u5f00zip\u6587\u4ef6\uff0c\u5e76\u8c03\u7528 <code>extractall<\/code> \u65b9\u6cd5\u5c06\u6240\u6709\u5185\u5bb9\u89e3\u538b\u5230\u6307\u5b9a\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u6807\u51c6\u5e93tarfile<\/h3>\n<\/p>\n<p><p>tarfile\u5e93\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u5904\u7406\u538b\u7f29\u6587\u4ef6\u7684\u6807\u51c6\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u5904\u7406.tar\u3001.tar.gz\u3001.tar.bz2\u7b49\u683c\u5f0f\u7684\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528tarfile\u5e93\u89e3\u538b\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tarfile<\/p>\n<p>def extract_tar_file(tar_file_path, extract_to):<\/p>\n<p>    with tarfile.open(tar_file_path, &#39;r:*&#39;) as tar_ref:<\/p>\n<p>        tar_ref.extractall(extract_to)<\/p>\n<p>    print(f&quot;Extracted all contents of {tar_file_path} to {extract_to}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>extract_tar_file(&#39;example.tar.gz&#39;, &#39;extracted_files&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>extract_tar_file<\/code> \u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1atar\u6587\u4ef6\u7684\u8def\u5f84\u548c\u89e3\u538b\u6587\u4ef6\u7684\u76ee\u6807\u76ee\u5f55\u3002\u4f7f\u7528 <code>tarfile.open<\/code> \u6253\u5f00tar\u6587\u4ef6\uff0c\u5e76\u8c03\u7528 <code>extractall<\/code> \u65b9\u6cd5\u5c06\u6240\u6709\u5185\u5bb9\u89e3\u538b\u5230\u6307\u5b9a\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93shutil<\/h3>\n<\/p>\n<p><p>shutil\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u5176\u4e2d\u5305\u62ec\u89e3\u538b\u7f29\u529f\u80fd\u3002shutil\u5e93\u53ef\u4ee5\u5904\u7406\u591a\u79cd\u538b\u7f29\u683c\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528shutil\u5e93\u89e3\u538b\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>def extract_archive_file(archive_file_path, extract_to):<\/p>\n<p>    shutil.unpack_archive(archive_file_path, extract_to)<\/p>\n<p>    print(f&quot;Extracted all contents of {archive_file_path} to {extract_to}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>extract_archive_file(&#39;example.zip&#39;, &#39;extracted_files&#39;)<\/p>\n<p>extract_archive_file(&#39;example.tar.gz&#39;, &#39;extracted_files&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>extract_archive_file<\/code> \u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u538b\u7f29\u6587\u4ef6\u7684\u8def\u5f84\u548c\u89e3\u538b\u6587\u4ef6\u7684\u76ee\u6807\u76ee\u5f55\u3002\u4f7f\u7528 <code>shutil.unpack_archive<\/code> \u65b9\u6cd5\u89e3\u538b\u6587\u4ef6\uff0c\u65e0\u9700\u6307\u5b9a\u6587\u4ef6\u683c\u5f0f\uff0cshutil\u4f1a\u81ea\u52a8\u68c0\u6d4b\u5e76\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93patool<\/h3>\n<\/p>\n<p><p>patool\u5e93\u662f\u4e00\u4e2a\u591a\u529f\u80fd\u7684\u538b\u7f29\u548c\u89e3\u538b\u7f29\u5de5\u5177\uff0c\u652f\u6301\u591a\u79cd\u538b\u7f29\u683c\u5f0f\u3002\u9700\u8981\u5148\u5b89\u88c5patool\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u89e3\u538b\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import patoolib<\/p>\n<p>def extract_with_patool(archive_file_path, extract_to):<\/p>\n<p>    patoolib.extract_archive(archive_file_path, outdir=extract_to)<\/p>\n<p>    print(f&quot;Extracted all contents of {archive_file_path} to {extract_to}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>extract_with_patool(&#39;example.zip&#39;, &#39;extracted_files&#39;)<\/p>\n<p>extract_with_patool(&#39;example.tar.gz&#39;, &#39;extracted_files&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>extract_with_patool<\/code> \u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u538b\u7f29\u6587\u4ef6\u7684\u8def\u5f84\u548c\u89e3\u538b\u6587\u4ef6\u7684\u76ee\u6807\u76ee\u5f55\u3002\u4f7f\u7528 <code>patoolib.extract_archive<\/code> \u65b9\u6cd5\u89e3\u538b\u6587\u4ef6\uff0cpatool\u81ea\u52a8\u5904\u7406\u5404\u79cd\u683c\u5f0f\u7684\u538b\u7f29\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001ZIPFILE\u5e93\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u89e3\u538b\u64cd\u4f5c\uff0czipfile\u5e93\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u6bd4\u5982\u5217\u51fazip\u6587\u4ef6\u5185\u5bb9\u3001\u89e3\u538b\u7279\u5b9a\u6587\u4ef6\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u9ad8\u7ea7\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>\u5217\u51faZIP\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ea\u60f3\u67e5\u770bzip\u6587\u4ef6\u4e2d\u5305\u542b\u54ea\u4e9b\u6587\u4ef6\uff0c\u800c\u4e0d\u9700\u8981\u89e3\u538b\u6240\u6709\u5185\u5bb9\u3002\u53ef\u4ee5\u4f7f\u7528 <code>zipfile.ZipFile.namelist()<\/code> \u65b9\u6cd5\u5217\u51fazip\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>def list_zip_contents(zip_file_path):<\/p>\n<p>    with zipfile.ZipFile(zip_file_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>        file_list = zip_ref.namelist()<\/p>\n<p>    return file_list<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>file_list = list_zip_contents(&#39;example.zip&#39;)<\/p>\n<p>print(file_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>list_zip_contents<\/code> \u51fd\u6570\u8fd4\u56dezip\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u540d\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u89e3\u538b\u7279\u5b9a\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ea\u9700\u8981\u89e3\u538bzip\u6587\u4ef6\u4e2d\u7684\u67d0\u4e9b\u7279\u5b9a\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528 <code>zipfile.ZipFile.extract<\/code> \u65b9\u6cd5\u53ea\u89e3\u538b\u6307\u5b9a\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>def extract_specific_file(zip_file_path, file_name, extract_to):<\/p>\n<p>    with zipfile.ZipFile(zip_file_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>        zip_ref.extract(file_name, extract_to)<\/p>\n<p>    print(f&quot;Extracted {file_name} from {zip_file_path} to {extract_to}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>extract_specific_file(&#39;example.zip&#39;, &#39;specific_file.txt&#39;, &#39;extracted_files&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>extract_specific_file<\/code> \u51fd\u6570\u53ea\u89e3\u538b\u6307\u5b9a\u7684\u6587\u4ef6\u5230\u76ee\u6807\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001TARFILE\u5e93\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u4e0ezipfile\u5e93\u7c7b\u4f3c\uff0ctarfile\u5e93\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u6bd4\u5982\u5217\u51fatar\u6587\u4ef6\u5185\u5bb9\u3001\u89e3\u538b\u7279\u5b9a\u6587\u4ef6\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u9ad8\u7ea7\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>\u5217\u51faTAR\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>tarfile.TarFile.getnames()<\/code> \u65b9\u6cd5\u5217\u51fatar\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tarfile<\/p>\n<p>def list_tar_contents(tar_file_path):<\/p>\n<p>    with tarfile.open(tar_file_path, &#39;r:*&#39;) as tar_ref:<\/p>\n<p>        file_list = tar_ref.getnames()<\/p>\n<p>    return file_list<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>file_list = list_tar_contents(&#39;example.tar.gz&#39;)<\/p>\n<p>print(file_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>list_tar_contents<\/code> \u51fd\u6570\u8fd4\u56detar\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u540d\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u89e3\u538b\u7279\u5b9a\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>tarfile.TarFile.extract<\/code> \u65b9\u6cd5\u53ea\u89e3\u538b\u6307\u5b9a\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tarfile<\/p>\n<p>def extract_specific_tar_file(tar_file_path, file_name, extract_to):<\/p>\n<p>    with tarfile.open(tar_file_path, &#39;r:*&#39;) as tar_ref:<\/p>\n<p>        tar_ref.extract(file_name, extract_to)<\/p>\n<p>    print(f&quot;Extracted {file_name} from {tar_file_path} to {extract_to}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>extract_specific_tar_file(&#39;example.tar.gz&#39;, &#39;specific_file.txt&#39;, &#39;extracted_files&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>extract_specific_tar_file<\/code> \u51fd\u6570\u53ea\u89e3\u538b\u6307\u5b9a\u7684\u6587\u4ef6\u5230\u76ee\u6807\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001SHUTIL\u5e93\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>shutil\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u9664\u4e86\u57fa\u672c\u7684\u89e3\u538b\u64cd\u4f5c\uff0c\u8fd8\u53ef\u4ee5\u590d\u5236\u3001\u79fb\u52a8\u6587\u4ef6\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u9ad8\u7ea7\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>\u590d\u5236\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>shutil.copy<\/code> \u65b9\u6cd5\u590d\u5236\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>def copy_file(src_file_path, dest_file_path):<\/p>\n<p>    shutil.copy(src_file_path, dest_file_path)<\/p>\n<p>    print(f&quot;Copied {src_file_path} to {dest_file_path}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>copy_file(&#39;example.txt&#39;, &#39;copy_of_example.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>copy_file<\/code> \u51fd\u6570\u590d\u5236\u6587\u4ef6\u5230\u76ee\u6807\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h4>\u79fb\u52a8\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>shutil.move<\/code> \u65b9\u6cd5\u79fb\u52a8\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>def move_file(src_file_path, dest_file_path):<\/p>\n<p>    shutil.move(src_file_path, dest_file_path)<\/p>\n<p>    print(f&quot;Moved {src_file_path} to {dest_file_path}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>move_file(&#39;example.txt&#39;, &#39;moved_example.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>move_file<\/code> \u51fd\u6570\u79fb\u52a8\u6587\u4ef6\u5230\u76ee\u6807\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001PATOOL\u5e93\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>patool\u5e93\u652f\u6301\u591a\u79cd\u538b\u7f29\u683c\u5f0f\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u538b\u7f29\u548c\u89e3\u538b\u529f\u80fd\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u9ad8\u7ea7\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u538b\u7f29\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>patoolib.create_archive<\/code> \u65b9\u6cd5\u521b\u5efa\u538b\u7f29\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import patoolib<\/p>\n<p>def create_archive(archive_file_path, files_to_compress):<\/p>\n<p>    patoolib.create_archive(archive_file_path, files_to_compress)<\/p>\n<p>    print(f&quot;Created archive {archive_file_path}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>create_archive(&#39;example.zip&#39;, [&#39;file1.txt&#39;, &#39;file2.txt&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>create_archive<\/code> \u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6307\u5b9a\u6587\u4ef6\u7684\u538b\u7f29\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u5217\u51fa\u538b\u7f29\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>patoolib.list_archive<\/code> \u65b9\u6cd5\u5217\u51fa\u538b\u7f29\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import patoolib<\/p>\n<p>def list_archive_contents(archive_file_path):<\/p>\n<p>    file_list = patoolib.list_archive(archive_file_path)<\/p>\n<p>    return file_list<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>file_list = list_archive_contents(&#39;example.zip&#39;)<\/p>\n<p>print(file_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>list_archive_contents<\/code> \u51fd\u6570\u8fd4\u56de\u538b\u7f29\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u540d\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u5185\u5b58\u548c\u6027\u80fd\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528\u5206\u5757\u8bfb\u53d6\u548c\u5199\u5165\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5927\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u5206\u5757\u8bfb\u53d6\u548c\u5199\u5165<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5206\u5757\u8bfb\u53d6\u548c\u5199\u5165\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5927\u6587\u4ef6\uff0c\u907f\u514d\u4e00\u6b21\u6027\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5bfc\u81f4\u5185\u5b58\u4e0d\u8db3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>def extract_large_zip_file(zip_file_path, extract_to, chunk_size=1024):<\/p>\n<p>    with zipfile.ZipFile(zip_file_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>        for file_info in zip_ref.infolist():<\/p>\n<p>            file_path = zip_ref.extract(file_info, extract_to)<\/p>\n<p>            with open(file_path, &#39;rb&#39;) as src_file:<\/p>\n<p>                with open(file_path, &#39;wb&#39;) as dest_file:<\/p>\n<p>                    while True:<\/p>\n<p>                        chunk = src_file.read(chunk_size)<\/p>\n<p>                        if not chunk:<\/p>\n<p>                            break<\/p>\n<p>                        dest_file.write(chunk)<\/p>\n<p>    print(f&quot;Extracted all contents of {zip_file_path} to {extract_to}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>extract_large_zip_file(&#39;example.zip&#39;, &#39;extracted_files&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>extract_large_zip_file<\/code> \u51fd\u6570\u4f7f\u7528\u5206\u5757\u8bfb\u53d6\u548c\u5199\u5165\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5927\u6587\u4ef6\uff0c\u907f\u514d\u4e00\u6b21\u6027\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5bfc\u81f4\u5185\u5b58\u4e0d\u8db3\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u5bc6\u7801\u4fdd\u62a4\u7684\u538b\u7f29\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6709\u4e9b\u538b\u7f29\u6587\u4ef6\u53ef\u80fd\u8bbe\u7f6e\u4e86\u5bc6\u7801\u4fdd\u62a4\uff0c\u9700\u8981\u63d0\u4f9b\u5bc6\u7801\u624d\u80fd\u89e3\u538b\u3002zipfile\u5e93\u548cpatool\u5e93\u90fd\u652f\u6301\u5904\u7406\u5bc6\u7801\u4fdd\u62a4\u7684\u538b\u7f29\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528ZIPFILE\u5e93\u5904\u7406\u5bc6\u7801\u4fdd\u62a4\u7684\u538b\u7f29\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>zipfile.ZipFile.setpassword<\/code> \u65b9\u6cd5\u8bbe\u7f6e\u5bc6\u7801\u6765\u89e3\u538b\u4fdd\u62a4\u7684zip\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>def extract_password_protected_zip(zip_file_path, extract_to, password):<\/p>\n<p>    with zipfile.ZipFile(zip_file_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>        zip_ref.setpassword(password.encode())<\/p>\n<p>        zip_ref.extractall(extract_to)<\/p>\n<p>    print(f&quot;Extracted all contents of {zip_file_path} to {extract_to}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>extract_password_protected_zip(&#39;example.zip&#39;, &#39;extracted_files&#39;, &#39;password123&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>extract_password_protected_zip<\/code> \u51fd\u6570\u63a5\u53d7\u5bc6\u7801\u53c2\u6570\uff0c\u5e76\u4f7f\u7528 <code>setpassword<\/code> \u65b9\u6cd5\u8bbe\u7f6e\u5bc6\u7801\u6765\u89e3\u538b\u4fdd\u62a4\u7684zip\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528PATOOL\u5e93\u5904\u7406\u5bc6\u7801\u4fdd\u62a4\u7684\u538b\u7f29\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>patoolib.extract_archive<\/code> \u65b9\u6cd5\u5e76\u4f20\u9012\u5bc6\u7801\u53c2\u6570\u6765\u89e3\u538b\u4fdd\u62a4\u7684\u538b\u7f29\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import patoolib<\/p>\n<p>def extract_password_protected_archive(archive_file_path, extract_to, password):<\/p>\n<p>    patoolib.extract_archive(archive_file_path, outdir=extract_to, verbosity=-1, password=password)<\/p>\n<p>    print(f&quot;Extracted all contents of {archive_file_path} to {extract_to}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>extract_password_protected_archive(&#39;example.zip&#39;, &#39;extracted_files&#39;, &#39;password123&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>extract_password_protected_archive<\/code> \u51fd\u6570\u63a5\u53d7\u5bc6\u7801\u53c2\u6570\uff0c\u5e76\u4f20\u9012\u7ed9 <code>extract_archive<\/code> \u65b9\u6cd5\u6765\u89e3\u538b\u4fdd\u62a4\u7684\u538b\u7f29\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u635f\u574f\u7684\u538b\u7f29\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u538b\u7f29\u6587\u4ef6\u53ef\u80fd\u4f1a\u635f\u574f\uff0c\u5bfc\u81f4\u65e0\u6cd5\u6b63\u5e38\u89e3\u538b\u3002\u53ef\u4ee5\u5c1d\u8bd5\u4fee\u590d\u635f\u574f\u7684\u538b\u7f29\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528ZIPFILE\u5e93\u4fee\u590d\u635f\u574f\u7684\u538b\u7f29\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>zipfile\u5e93\u672c\u8eab\u4e0d\u63d0\u4f9b\u4fee\u590d\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u91cd\u5efa\u7d22\u5f15\u7684\u65b9\u5f0f\u5c1d\u8bd5\u4fee\u590d\u635f\u574f\u7684zip\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>def rep<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>r_zip_file(zip_file_path, repaired_zip_file_path):<\/p>\n<p>    with zipfile.ZipFile(zip_file_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>        with zipfile.ZipFile(repaired_zip_file_path, &#39;w&#39;) as new_zip_ref:<\/p>\n<p>            for file_info in zip_ref.infolist():<\/p>\n<p>                try:<\/p>\n<p>                    data = zip_ref.read(file_info.filename)<\/p>\n<p>                    new_zip_ref.writestr(file_info, data)<\/p>\n<p>                except zipfile.BadZipFile:<\/p>\n<p>                    print(f&quot;Skipping damaged file: {file_info.filename}&quot;)<\/p>\n<p>    print(f&quot;Repaired zip file saved as {repaired_zip_file_path}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>repair_zip_file(&#39;damaged_example.zip&#39;, &#39;repaired_example.zip&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>repair_zip_file<\/code> \u51fd\u6570\u5c1d\u8bd5\u8bfb\u53d6\u6bcf\u4e2a\u6587\u4ef6\u5e76\u91cd\u5efa\u7d22\u5f15\uff0c\u8df3\u8fc7\u635f\u574f\u7684\u6587\u4ef6\u5e76\u4fdd\u5b58\u4e3a\u65b0\u7684zip\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7Python\u89e3\u538b\u6587\u4ef6\u5305\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u538b\u7f29\u6587\u4ef6\u7684\u683c\u5f0f\u3002<strong>\u4f7f\u7528\u6807\u51c6\u5e93zipfile\u3001\u4f7f\u7528\u6807\u51c6\u5e93tarfile\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93shutil\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93patool<\/strong> \u90fd\u662f\u975e\u5e38\u6709\u6548\u7684\u65b9\u6cd5\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u538b\u7f29\u6587\u4ef6\uff0c\u89e3\u51b3\u5728\u5de5\u4f5c\u548c\u9879\u76ee\u4e2d\u9047\u5230\u7684\u5b9e\u9645\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u89e3\u538b\u7279\u5b9a\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u5982ZIP\u6216RAR\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u538b\u7f29\u6587\u4ef6\u3002\u5bf9\u4e8eZIP\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>zipfile<\/code>\u6a21\u5757\u3002\u53ea\u9700\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u6253\u5f00ZIP\u6587\u4ef6\uff0c\u8c03\u7528<code>extractall()<\/code>\u65b9\u6cd5\u5373\u53ef\u89e3\u538b\u6240\u6709\u5185\u5bb9\u3002\u5bf9\u4e8eRAR\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>rarfile<\/code>\u5e93\uff0c\u5b83\u9700\u8981\u5148\u5b89\u88c5\u3002\u786e\u4fdd\u60a8\u5df2\u7ecf\u5b89\u88c5\u4e86\u5fc5\u8981\u7684\u5e93\uff0c\u4f8b\u5982\u4f7f\u7528<code>pip install rarfile<\/code>\u547d\u4ee4\u3002<\/p>\n<p><strong>\u5728\u89e3\u538b\u6587\u4ef6\u65f6\u5982\u4f55\u5904\u7406\u6587\u4ef6\u8986\u76d6\u7684\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u89e3\u538b\u6587\u4ef6\u65f6\uff0c\u5982\u679c\u76ee\u6807\u76ee\u5f55\u4e2d\u5df2\u7ecf\u5b58\u5728\u540c\u540d\u6587\u4ef6\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>extractall()<\/code>\u65b9\u6cd5\u4f1a\u8986\u76d6\u8fd9\u4e9b\u6587\u4ef6\u3002\u5982\u679c\u5e0c\u671b\u907f\u514d\u8986\u76d6\uff0c\u53ef\u4ee5\u5728\u89e3\u538b\u4e4b\u524d\u68c0\u67e5\u76ee\u6807\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u662f\u5426\u5df2\u5b58\u5728\uff0c\u5e76\u6839\u636e\u9700\u8981\u9009\u62e9\u8df3\u8fc7\u6216\u91cd\u547d\u540d\u3002\u5b9e\u73b0\u8fd9\u4e00\u6b65\u9aa4\u9700\u8981\u624b\u52a8\u7f16\u5199\u4ee3\u7801\u6765\u8fdb\u884c\u6587\u4ef6\u5b58\u5728\u68c0\u67e5\u3002<\/p>\n<p><strong>Python\u89e3\u538b\u6587\u4ef6\u65f6\u5982\u4f55\u5904\u7406\u5bc6\u7801\u4fdd\u62a4\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5bc6\u7801\u4fdd\u62a4\u7684ZIP\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>zipfile<\/code>\u6a21\u5757\u4e2d\u7684<code>extractall()<\/code>\u65b9\u6cd5\uff0c\u63d0\u4f9b<code>pwd<\/code>\u53c2\u6570\u6765\u8f93\u5165\u5bc6\u7801\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>zipfile<\/code>\u6a21\u5757\u4e0d\u652f\u6301RAR\u6587\u4ef6\u7684\u89e3\u538b\uff0c\u56e0\u6b64\u5bf9\u4e8eRAR\u683c\u5f0f\uff0c\u60a8\u5fc5\u987b\u4f7f\u7528<code>rarfile<\/code>\u5e93\uff0c\u5e76\u901a\u8fc7\u76f8\u5e94\u7684\u65b9\u6cd5\u6765\u63d0\u4f9b\u5bc6\u7801\u8fdb\u884c\u89e3\u538b\u3002\u786e\u4fdd\u4f60\u4e86\u89e3\u4f7f\u7528\u7684\u5e93\u7684\u76f8\u5173\u6587\u6863\uff0c\u4ee5\u4fbf\u6b63\u786e\u5b9e\u65bd\u5bc6\u7801\u89e3\u538b\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7Python\u89e3\u538b\u6587\u4ef6\u5305\u7684\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6807\u51c6\u5e93zipfile\u3001\u4f7f\u7528\u6807\u51c6\u5e93tarfile\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93sh [&hellip;]","protected":false},"author":3,"featured_media":1168122,"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\/1168121"}],"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=1168121"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168121\/revisions"}],"predecessor-version":[{"id":1168125,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168121\/revisions\/1168125"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1168122"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1168121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1168121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1168121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}