{"id":946860,"date":"2024-12-26T23:46:27","date_gmt":"2024-12-26T15:46:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/946860.html"},"modified":"2024-12-26T23:46:30","modified_gmt":"2024-12-26T15:46:30","slug":"%e5%a6%82%e4%bd%95%e8%a7%a3%e5%8e%8bpython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/946860.html","title":{"rendered":"\u5982\u4f55\u89e3\u538bPython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082845\/9fa71a66-4e12-4906-b561-fa12b55be919.webp\" alt=\"\u5982\u4f55\u89e3\u538bPython\" \/><\/p>\n<p><p> <strong>\u89e3\u538bPython\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6ezipfile\u6a21\u5757\u3001\u4f7f\u7528\u5185\u7f6etarfile\u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982unrar\u3001\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u547d\u4ee4\u884c\u5de5\u5177<\/strong>\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528Python\u5185\u7f6e\u7684<code>zipfile<\/code>\u6a21\u5757\u6765\u89e3\u538bzip\u6587\u4ef6\u662f\u6700\u5e38\u7528\u548c\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u901a\u8fc7<code>zipfile.ZipFile<\/code>\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u6253\u5f00\u3001\u8bfb\u53d6\u3001\u5199\u5165\u548c\u5173\u95edzip\u6587\u4ef6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e00\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>zipfile<\/code>\u6a21\u5757\u89e3\u538bzip\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u5bfc\u5165\u6a21\u5757\uff0c\u7136\u540e\u901a\u8fc7<code>zipfile.ZipFile<\/code>\u7c7b\u521b\u5efa\u4e00\u4e2aZipFile\u5bf9\u8c61\uff0c\u63a5\u4e0b\u6765\u4f7f\u7528<code>extractall()<\/code>\u65b9\u6cd5\u5c06\u6587\u4ef6\u89e3\u538b\u5230\u6307\u5b9a\u76ee\u5f55\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>def unzip_file(zip_file_path, extract_to_path):<\/p>\n<p>    with zipfile.ZipFile(zip_file_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>        zip_ref.extractall(extract_to_path)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>unzip_file(&#39;example.zip&#39;, &#39;extracted_folder&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>example.zip<\/code>\u662f\u8981\u89e3\u538b\u7684\u6587\u4ef6\u540d\uff0c<code>extracted_folder<\/code>\u662f\u76ee\u6807\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528ZIPFILE\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684<code>zipfile<\/code>\u6a21\u5757\u662f\u5904\u7406zip\u6587\u4ef6\u7684\u6807\u51c6\u5de5\u5177\u3002\u5b83\u63d0\u4f9b\u4e86\u521b\u5efa\u3001\u8bfb\u53d6\u3001\u5199\u5165\u3001\u8ffd\u52a0\u548c\u89e3\u538b\u7f29zip\u6587\u4ef6\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efaZip\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684zip\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ZipFile<\/code>\u5bf9\u8c61\u5e76\u4f20\u9012<code>&#39;w&#39;<\/code>\u6a21\u5f0f\u53c2\u6570\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7<code>write()<\/code>\u65b9\u6cd5\u5c06\u6587\u4ef6\u6dfb\u52a0\u5230zip\u6863\u6848\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>def create_zip(file_paths, zip_file_path):<\/p>\n<p>    with zipfile.ZipFile(zip_file_path, &#39;w&#39;) as zipf:<\/p>\n<p>        for file in file_paths:<\/p>\n<p>            zipf.write(file)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>create_zip([&#39;file1.txt&#39;, &#39;file2.txt&#39;], &#39;archive.zip&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u89e3\u538bZip\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u5982\u524d\u6240\u8ff0\uff0c<code>extractall()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u6240\u6709\u5185\u5bb9\u89e3\u538b\u5230\u6307\u5b9a\u76ee\u5f55\u3002<code>extract()<\/code>\u65b9\u6cd5\u5219\u7528\u4e8e\u89e3\u538b\u5355\u4e2a\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>def extract_zip(zip_file_path, extract_to_path):<\/p>\n<p>    with zipfile.ZipFile(zip_file_path, &#39;r&#39;) as zip_ref:<\/p>\n<p>        zip_ref.extractall(extract_to_path)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>extract_zip(&#39;archive.zip&#39;, &#39;output_folder&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8bfb\u53d6Zip\u6587\u4ef6\u5185\u5bb9<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>namelist()<\/code>\u65b9\u6cd5\u83b7\u53d6zip\u6587\u4ef6\u4e2d\u6240\u6709\u6587\u4ef6\u7684\u5217\u8868\u3002<\/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>        return zip_ref.namelist()<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>contents = list_zip_contents(&#39;archive.zip&#39;)<\/p>\n<p>print(contents)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528TARFILE\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u9664\u4e86zip\u6587\u4ef6\uff0cPython\u7684<code>tarfile<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u5904\u7406tar\u3001tar.gz\u548ctar.bz2\u7b49\u683c\u5f0f\u7684\u538b\u7f29\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efaTar\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>tarfile.open()<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2atar\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7<code>add()<\/code>\u65b9\u6cd5\u6dfb\u52a0\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tarfile<\/p>\n<p>def create_tar(file_paths, tar_file_path):<\/p>\n<p>    with tarfile.open(tar_file_path, &#39;w&#39;) as tar:<\/p>\n<p>        for file in file_paths:<\/p>\n<p>            tar.add(file)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>create_tar([&#39;file1.txt&#39;, &#39;file2.txt&#39;], &#39;archive.tar&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u89e3\u538bTar\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>extractall()<\/code>\u65b9\u6cd5\u89e3\u538btar\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tarfile<\/p>\n<p>def extract_tar(tar_file_path, extract_to_path):<\/p>\n<p>    with tarfile.open(tar_file_path, &#39;r&#39;) as tar:<\/p>\n<p>        tar.extractall(extract_to_path)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>extract_tar(&#39;archive.tar&#39;, &#39;output_folder&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8bfb\u53d6Tar\u6587\u4ef6\u5185\u5bb9<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>getnames()<\/code>\u65b9\u6cd5\u5217\u51fatar\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u3002<\/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:<\/p>\n<p>        return tar.getnames()<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>contents = list_tar_contents(&#39;archive.tar&#39;)<\/p>\n<p>print(contents)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u683c\u5f0f\u5982rar\uff0cPython\u6807\u51c6\u5e93\u6ca1\u6709\u76f4\u63a5\u652f\u6301\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>unrar<\/code>\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5unrar<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5<code>unrar<\/code>\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u5b8c\u6210\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install unrar<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528unrar\u89e3\u538brar\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>unrar<\/code>\u5e93\u89e3\u538brar\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from unrar import rarfile<\/p>\n<p>def extract_rar(rar_file_path, extract_to_path):<\/p>\n<p>    with rarfile.RarFile(rar_file_path) as rar:<\/p>\n<p>        rar.extractall(extract_to_path)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>extract_rar(&#39;archive.rar&#39;, &#39;output_folder&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u547d\u4ee4\u884c\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u547d\u4ee4\u884c\u5de5\u5177\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u89e3\u538b\u7f29\u64cd\u4f5c\u3002Python\u53ef\u4ee5\u901a\u8fc7<code>subprocess<\/code>\u6a21\u5757\u8c03\u7528\u8fd9\u4e9b\u547d\u4ee4\u884c\u5de5\u5177\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528unzip\u547d\u4ee4\u89e3\u538b<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Linux\u6216Mac\u4e0a\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>unzip<\/code>\u547d\u4ee4\u89e3\u538b\u7f29\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def unzip_with_command(zip_file_path, extract_to_path):<\/p>\n<p>    subprocess.run([&#39;unzip&#39;, zip_file_path, &#39;-d&#39;, extract_to_path])<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>unzip_with_command(&#39;archive.zip&#39;, &#39;output_folder&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528tar\u547d\u4ee4\u89e3\u538b<\/strong><\/li>\n<\/ol>\n<p><p>\u540c\u6837\u5730\uff0c<code>tar<\/code>\u547d\u4ee4\u53ef\u7528\u4e8e\u89e3\u538btar\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def untar_with_command(tar_file_path, extract_to_path):<\/p>\n<p>    subprocess.run([&#39;tar&#39;, &#39;-xf&#39;, tar_file_path, &#39;-C&#39;, extract_to_path])<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>untar_with_command(&#39;archive.tar&#39;, &#39;output_folder&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u8fd9\u4e9b\u65b9\u6cd5\u63d0\u4f9b\u4e86\u5728Python\u4e2d\u89e3\u538b\u5404\u79cd\u538b\u7f29\u683c\u5f0f\u6587\u4ef6\u7684\u591a\u79cd\u89e3\u51b3\u65b9\u6848\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6587\u4ef6\u683c\u5f0f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u786e\u4fdd\u5728\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u65f6\u5148\u5b89\u88c5\u76f8\u5e94\u7684\u4f9d\u8d56\u5305\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0cPython\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u538b\u7f29\u6587\u4ef6\uff0c\u4e3a\u6570\u636e\u5904\u7406\u548c\u7ba1\u7406\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u4fbf\u5229\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u89e3\u538bPython\u6587\u4ef6\u9700\u8981\u4ec0\u4e48\u5de5\u5177\uff1f<\/strong><br \/>\u89e3\u538bPython\u6587\u4ef6\u901a\u5e38\u4f7f\u7528\u538b\u7f29\u8f6f\u4ef6\uff0c\u5982WinRAR\u30017-Zip\u6216\u7cfb\u7edf\u81ea\u5e26\u7684\u89e3\u538b\u5de5\u5177\u3002\u53ea\u9700\u53f3\u952e\u70b9\u51fb\u538b\u7f29\u6587\u4ef6\uff0c\u9009\u62e9\u76f8\u5e94\u7684\u89e3\u538b\u9009\u9879\uff0c\u4fbf\u53ef\u4ee5\u5c06\u6587\u4ef6\u89e3\u538b\u5230\u6307\u5b9a\u76ee\u5f55\u3002<\/p>\n<p><strong>\u89e3\u538bPython\u5305\u540e\uff0c\u6211\u8be5\u5982\u4f55\u5b89\u88c5\uff1f<\/strong><br \/>\u89e3\u538bPython\u5305\u540e\uff0c\u901a\u5e38\u4f1a\u5728\u5305\u76ee\u5f55\u4e2d\u627e\u5230<code>setup.py<\/code>\u6587\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u8fdb\u5165\u8be5\u76ee\u5f55\uff0c\u5e76\u8fd0\u884c<code>python setup.py install<\/code>\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u53ca\u5176\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u5982pip\u3002<\/p>\n<p><strong>\u89e3\u538b\u540e\u7684Python\u6587\u4ef6\u80fd\u5426\u76f4\u63a5\u8fd0\u884c\uff1f<\/strong><br \/>\u89e3\u538b\u540e\u7684Python\u6587\u4ef6\u80fd\u5426\u76f4\u63a5\u8fd0\u884c\u53d6\u51b3\u4e8e\u6587\u4ef6\u7c7b\u578b\u3002\u5982\u679c\u662f\u53ef\u6267\u884c\u7684\u811a\u672c\u6587\u4ef6\uff08\u5982.py\u6587\u4ef6\uff09\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528<code>python \u6587\u4ef6\u540d.py<\/code>\u547d\u4ee4\u8fd0\u884c\u3002\u4f46\u5982\u679c\u7f3a\u5c11\u4f9d\u8d56\u5e93\uff0c\u53ef\u80fd\u9700\u8981\u5148\u5b89\u88c5\u76f8\u5173\u7684\u5e93\u6216\u5305\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u89e3\u538bPython\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6ezipfile\u6a21\u5757\u3001\u4f7f\u7528\u5185\u7f6etarfile\u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982unra [&hellip;]","protected":false},"author":3,"featured_media":946871,"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\/946860"}],"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=946860"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/946860\/revisions"}],"predecessor-version":[{"id":946872,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/946860\/revisions\/946872"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/946871"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=946860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=946860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=946860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}