{"id":982529,"date":"2024-12-27T07:11:33","date_gmt":"2024-12-26T23:11:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/982529.html"},"modified":"2024-12-27T07:11:35","modified_gmt":"2024-12-26T23:11:35","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%96%87%e4%bb%b6%e5%a4%8d%e5%88%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/982529.html","title":{"rendered":"\u5982\u4f55\u7528python\u6587\u4ef6\u590d\u5236"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210952\/ea73cec0-28f7-467a-81e5-c4e14b598cbf.webp\" alt=\"\u5982\u4f55\u7528python\u6587\u4ef6\u590d\u5236\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528shutil\u6a21\u5757\u3001os\u6a21\u5757\u3001\u4ee5\u53capandas\u5e93\u6765\u590d\u5236\u6587\u4ef6\u3002shutil\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u65b9\u6cd5\u3001os\u6a21\u5757\u63d0\u4f9b\u5e95\u5c42\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u3001\u800cpandas\u5e93\u5219\u53ef\u7528\u4e8e\u5904\u7406\u7279\u5b9a\u6570\u636e\u683c\u5f0f\u7684\u6587\u4ef6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u6587\u4ef6\u590d\u5236\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528SHUTIL\u6a21\u5757\u590d\u5236\u6587\u4ef6<\/p>\n<\/p>\n<p><p>shutil\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u6267\u884c\u9ad8\u7ea7\u6587\u4ef6\u64cd\u4f5c\u7684\u6807\u51c6\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5305\u62ec\u590d\u5236\u3001\u79fb\u52a8\u3001\u5220\u9664\u7b49\u529f\u80fd\u3002\u4f7f\u7528shutil.copy()\u6216shutil.copy2()\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5730\u590d\u5236\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528shutil.copy()<\/strong><\/p>\n<\/p>\n<p><p>shutil.copy()\u51fd\u6570\u7528\u4e8e\u590d\u5236\u6587\u4ef6\u7684\u5185\u5bb9\u548c\u6743\u9650\u3002\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u6e90\u6587\u4ef6\u8def\u5f84\u548c\u76ee\u6807\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>source_file = &#39;source.txt&#39;<\/p>\n<p>destination_file = &#39;destination.txt&#39;<\/p>\n<p>shutil.copy(source_file, destination_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0csource.txt\u6587\u4ef6\u88ab\u590d\u5236\u5230destination.txt\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528shutil.copy2()<\/strong><\/p>\n<\/p>\n<p><p>shutil.copy2()\u51fd\u6570\u4e0eshutil.copy()\u7c7b\u4f3c\uff0c\u4f46\u5b83\u8fd8\u4f1a\u590d\u5236\u6587\u4ef6\u7684\u5143\u6570\u636e\uff08\u4f8b\u5982\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u3001\u4fee\u6539\u65f6\u95f4\u7b49\uff09\u3002\u8fd9\u5728\u9700\u8981\u4fdd\u7559\u6587\u4ef6\u7684\u8be6\u7ec6\u4fe1\u606f\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>source_file = &#39;source.txt&#39;<\/p>\n<p>destination_file = &#39;destination.txt&#39;<\/p>\n<p>shutil.copy2(source_file, destination_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0eshutil.copy()\u7684\u533a\u522b\u5728\u4e8e\u5b83\u4fdd\u7559\u4e86\u66f4\u591a\u7684\u6587\u4ef6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528OS\u6a21\u5757\u590d\u5236\u6587\u4ef6<\/p>\n<\/p>\n<p><p>os\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5e95\u5c42\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u6587\u4ef6\u7684\u590d\u5236\u3002\u4e0d\u8fc7\uff0c\u8fd9\u79cd\u65b9\u5f0f\u6bd4\u8f83\u7e41\u7410\uff0c\u4e0d\u5982\u4f7f\u7528shutil\u6a21\u5757\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6253\u5f00\u548c\u5199\u5165\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528os\u6a21\u5757\u590d\u5236\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u624b\u52a8\u6253\u5f00\u6e90\u6587\u4ef6\u548c\u76ee\u6807\u6587\u4ef6\uff0c\u7136\u540e\u5c06\u6570\u636e\u4ece\u6e90\u6587\u4ef6\u8bfb\u51fa\u5e76\u5199\u5165\u76ee\u6807\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>source_file = &#39;source.txt&#39;<\/p>\n<p>destination_file = &#39;destination.txt&#39;<\/p>\n<p>with open(source_file, &#39;rb&#39;) as sf:<\/p>\n<p>    with open(destination_file, &#39;wb&#39;) as df:<\/p>\n<p>        while True:<\/p>\n<p>            chunk = sf.read(1024)<\/p>\n<p>            if not chunk:<\/p>\n<p>                break<\/p>\n<p>            df.write(chunk)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6e90\u6587\u4ef6\u88ab\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\uff0c\u5e76\u5c06\u5185\u5bb9\u4e00\u5757\u4e00\u5757\u5730\u8bfb\u53d6\u5e76\u5199\u5165\u76ee\u6807\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528PANDAS\u5e93\u590d\u5236\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u867d\u7136pandas\u5e93\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u7279\u5b9a\u6570\u636e\u683c\u5f0f\u7684\u6587\u4ef6\u590d\u5236\uff0c\u4f8b\u5982CSV\u6587\u4ef6\u3002\u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u590d\u5236\u548c\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u590d\u5236CSV\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>pandas\u5e93\u63d0\u4f9b\u4e86read_csv()\u548cto_csv()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u8bfb\u53d6\u548c\u5199\u5165CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>source_file = &#39;source.csv&#39;<\/p>\n<p>destination_file = &#39;destination.csv&#39;<\/p>\n<p>df = pd.read_csv(source_file)<\/p>\n<p>df.to_csv(destination_file, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cCSV\u6587\u4ef6\u88ab\u8bfb\u53d6\u5230\u4e00\u4e2aDataFrame\u5bf9\u8c61\u4e2d\uff0c\u7136\u540e\u5199\u5165\u5230\u76ee\u6807\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u590d\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u9009\u62e9\uff0c\u5177\u4f53\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u3002<strong>shutil\u6a21\u5757\u63d0\u4f9b\u4e86\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u6587\u4ef6\u590d\u5236\u65b9\u6cd5<\/strong>\uff0c\u9002\u5408\u5927\u591a\u6570\u5e38\u89c1\u7684\u6587\u4ef6\u64cd\u4f5c\u9700\u6c42\uff1b<strong>os\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u5e95\u5c42\u7684\u63a7\u5236<\/strong>\uff0c\u9002\u5408\u9700\u8981\u81ea\u5b9a\u4e49\u590d\u5236\u903b\u8f91\u7684\u60c5\u51b5\uff1b\u800c<strong>pandas\u5e93\u5219\u9002\u5408\u7528\u4e8e\u5904\u7406\u7279\u5b9a\u683c\u5f0f\u7684\u6570\u636e\u6587\u4ef6<\/strong>\u3002\u5728\u9009\u62e9\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u6839\u636e\u6587\u4ef6\u7c7b\u578b\u3001\u662f\u5426\u9700\u8981\u4fdd\u7559\u5143\u6570\u636e\u4ee5\u53ca\u6587\u4ef6\u64cd\u4f5c\u7684\u590d\u6742\u6027\u6765\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u590d\u5236\u6587\u4ef6\u7684\u5177\u4f53\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u590d\u5236\u6587\u4ef6\u7684\u8fc7\u7a0b\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684<code>shutil<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>shutil.copy()<\/code>\u6216<code>shutil.copyfile()<\/code>\u51fd\u6570\u3002<code>shutil.copy()<\/code>\u4e0d\u4ec5\u4f1a\u590d\u5236\u6587\u4ef6\u5185\u5bb9\uff0c\u8fd8\u4f1a\u4fdd\u7559\u539f\u6587\u4ef6\u7684\u6743\u9650\uff0c\u800c<code>shutil.copyfile()<\/code>\u4ec5\u590d\u5236\u6587\u4ef6\u5185\u5bb9\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import shutil\n\nshutil.copy(&#39;\u6e90\u6587\u4ef6\u8def\u5f84&#39;, &#39;\u76ee\u6807\u6587\u4ef6\u8def\u5f84&#39;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u590d\u5236\u4e4b\u524d\uff0c\u6e90\u6587\u4ef6\u8def\u5f84\u662f\u6b63\u786e\u7684\uff0c\u5e76\u4e14\u76ee\u6807\u8def\u5f84\u7684\u76ee\u5f55\u5b58\u5728\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Python\u590d\u5236\u6587\u4ef6\u5939\u53ca\u5176\u5185\u5bb9\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u63d0\u4f9b\u4e86<code>shutil.copytree()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u590d\u5236\u6574\u4e2a\u6587\u4ef6\u5939\u53ca\u5176\u6240\u6709\u5185\u5bb9\u3002\u4f7f\u7528\u65f6\u9700\u8981\u6307\u5b9a\u6e90\u6587\u4ef6\u5939\u548c\u76ee\u6807\u6587\u4ef6\u5939\u7684\u8def\u5f84\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import shutil\n\nshutil.copytree(&#39;\u6e90\u6587\u4ef6\u5939\u8def\u5f84&#39;, &#39;\u76ee\u6807\u6587\u4ef6\u5939\u8def\u5f84&#39;)\n<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u76ee\u6807\u6587\u4ef6\u5939\u5df2\u7ecf\u5b58\u5728\uff0c<code>copytree()<\/code>\u4f1a\u5f15\u53d1\u9519\u8bef\uff0c\u56e0\u6b64\u5728\u590d\u5236\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u76ee\u6807\u6587\u4ef6\u5939\u4e0d\u5b58\u5728\uff0c\u6216\u4f7f\u7528<code>dirs_exist_ok=True<\/code>\u53c2\u6570\u6765\u5141\u8bb8\u8986\u76d6\u3002<\/p>\n<p><strong>\u5728\u590d\u5236\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6587\u4ef6\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6587\u4ef6\u590d\u5236\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u5f02\u5e38\u60c5\u51b5\u3002\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u5e76\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\u3002\u4ee5\u4e0b\u662f\u5904\u7406\u5f02\u5e38\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import shutil\n\ntry:\n    shutil.copy(&#39;\u6e90\u6587\u4ef6\u8def\u5f84&#39;, &#39;\u76ee\u6807\u6587\u4ef6\u8def\u5f84&#39;)\nexcept FileNotFoundError:\n    print(&quot;\u6e90\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u8bf7\u68c0\u67e5\u8def\u5f84\u3002&quot;)\nexcept PermissionError:\n    print(&quot;\u6ca1\u6709\u6743\u9650\u590d\u5236\u6587\u4ef6\uff0c\u8bf7\u68c0\u67e5\u6587\u4ef6\u6743\u9650\u3002&quot;)\nexcept Exception as e:\n    print(f&quot;\u590d\u5236\u6587\u4ef6\u65f6\u53d1\u751f\u9519\u8bef\uff1a{e}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u63a7\u5236\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\uff0c\u5e76\u53ca\u65f6\u5904\u7406\u6f5c\u5728\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528shutil\u6a21\u5757\u3001os\u6a21\u5757\u3001\u4ee5\u53capandas\u5e93\u6765\u590d\u5236\u6587\u4ef6\u3002shutil\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355 [&hellip;]","protected":false},"author":3,"featured_media":982536,"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\/982529"}],"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=982529"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982529\/revisions"}],"predecessor-version":[{"id":982537,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982529\/revisions\/982537"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/982536"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=982529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=982529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=982529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}