{"id":970189,"date":"2024-12-27T05:23:48","date_gmt":"2024-12-26T21:23:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/970189.html"},"modified":"2024-12-27T05:23:50","modified_gmt":"2024-12-26T21:23:50","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%a4%8d%e5%88%b6%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/970189.html","title":{"rendered":"\u5982\u4f55\u7528python\u590d\u5236\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24184111\/392d48aa-229d-459f-b62f-597fc7a006a8.webp\" alt=\"\u5982\u4f55\u7528python\u590d\u5236\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u590d\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528shutil\u6a21\u5757\u3001\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528pandas\u8bfb\u53d6\u5199\u5165\u3001\u4f7f\u7528pathlib\u6a21\u5757\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0cshutil\u6a21\u5757\u6700\u4e3a\u5e38\u7528\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u4e14\u9ad8\u6548\u7684\u6587\u4ef6\u590d\u5236\u529f\u80fd\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528shutil\u6a21\u5757\u6765\u590d\u5236\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528SHUTIL\u6a21\u5757<\/h3>\n<\/p>\n<p><p>shutil\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u7528\u4e8e\u6587\u4ef6\u64cd\u4f5c\u7684\u9ad8\u7ea7\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u4fbf\u6377\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u7684\u590d\u5236\u3001\u79fb\u52a8\u3001\u91cd\u547d\u540d\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u590d\u5236\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528shutil\u6a21\u5757\u590d\u5236\u6587\u4ef6\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8c03\u7528<code>shutil.copy()<\/code>\u51fd\u6570\u5373\u53ef\u3002\u6b64\u51fd\u6570\u9700\u8981\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<h2><strong>\u5b9a\u4e49\u6e90\u6587\u4ef6\u548c\u76ee\u6807\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>source_file = &#39;source.txt&#39;<\/p>\n<p>destination_file = &#39;destination.txt&#39;<\/p>\n<h2><strong>\u590d\u5236\u6587\u4ef6<\/strong><\/h2>\n<p>shutil.copy(source_file, destination_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>source.txt<\/code>\u6587\u4ef6\u5c06\u88ab\u590d\u5236\u5230<code>destination.txt<\/code>\u3002\u5982\u679c\u76ee\u6807\u6587\u4ef6\u5df2\u7ecf\u5b58\u5728\uff0c\u5b83\u5c06\u88ab\u8986\u76d6\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u590d\u5236\u6587\u4ef6\u5e76\u4fdd\u7559\u5143\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5728\u590d\u5236\u6587\u4ef6\u65f6\u4fdd\u7559\u6587\u4ef6\u7684\u5143\u6570\u636e\uff08\u5982\u6743\u9650\u3001\u65f6\u95f4\u6233\u7b49\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shutil.copy2()<\/code>\u51fd\u6570\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><code>shutil.copy2()<\/code>\u4e0e<code>shutil.copy()<\/code>\u7684\u4f7f\u7528\u65b9\u6cd5\u76f8\u540c\uff0c\u4f46\u5b83\u4f1a\u4fdd\u7559\u6587\u4ef6\u7684\u5143\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528OS\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u867d\u7136shutil\u6a21\u5757\u5df2\u7ecf\u53ef\u4ee5\u5f88\u597d\u5730\u5904\u7406\u6587\u4ef6\u590d\u5236\uff0c\u4f46os\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u5e95\u5c42\u7684\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528OS\u6a21\u5757\u590d\u5236\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>os\u6a21\u5757\u672c\u8eab\u5e76\u6ca1\u6709\u76f4\u63a5\u7528\u4e8e\u590d\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\uff0c\u4f46\u53ef\u4ee5\u7ed3\u5408\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u6765\u5b9e\u73b0\u6587\u4ef6\u590d\u5236\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<h2><strong>\u6253\u5f00\u6e90\u6587\u4ef6\u548c\u76ee\u6807\u6587\u4ef6<\/strong><\/h2>\n<p>with open(source_file, &#39;rb&#39;) as src, open(destination_file, &#39;wb&#39;) as dst:<\/p>\n<p>    # \u8bfb\u53d6\u6e90\u6587\u4ef6\u5185\u5bb9\u5e76\u5199\u5165\u76ee\u6807\u6587\u4ef6<\/p>\n<p>    dst.write(src.read())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e0d\u4f7f\u7528\u989d\u5916\u6a21\u5757\u7684\u60c5\u51b5\u4e0b\u5b9e\u73b0\u6587\u4ef6\u590d\u5236\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u590d\u5236\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528PANDAS\u8bfb\u53d6\u548c\u5199\u5165<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u7279\u5b9a\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u5982CSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u6765\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\uff0c\u4ece\u800c\u5b9e\u73b0\u6587\u4ef6\u590d\u5236\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528Pandas\u590d\u5236CSV\u6587\u4ef6<\/h4>\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<h2><strong>\u8bfb\u53d6\u6e90CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(source_file)<\/p>\n<h2><strong>\u5199\u5165\u76ee\u6807CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data.to_csv(destination_file, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u590d\u5236\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5bf9\u6587\u4ef6\u5185\u5bb9\u8fdb\u884c\u64cd\u4f5c\u548c\u5904\u7406\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528PATHLIB\u6a21\u5757<\/h3>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u8def\u5f84\u7684\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528Pathlib\u590d\u5236\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u672c\u8eab\u4e0d\u63d0\u4f9b\u76f4\u63a5\u7684\u6587\u4ef6\u590d\u5236\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u7ed3\u5408shutil\u6a21\u5757\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>import shutil<\/p>\n<p>source_file = Path(&#39;source.txt&#39;)<\/p>\n<p>destination_file = Path(&#39;destination.txt&#39;)<\/p>\n<h2><strong>\u4f7f\u7528shutil\u590d\u5236\u6587\u4ef6<\/strong><\/h2>\n<p>shutil.copy(source_file, destination_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5229\u7528pathlib\u6a21\u5757\u7684\u8def\u5f84\u64cd\u4f5c\u529f\u80fd\uff0c\u540c\u65f6\u4f7f\u7528shutil\u6a21\u5757\u7684\u6587\u4ef6\u590d\u5236\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6587\u4ef6\u590d\u5236\u65f6\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u590d\u5236\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u4ee5\u786e\u4fdd\u590d\u5236\u64cd\u4f5c\u7684\u6210\u529f\u548c\u5b89\u5168\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u8def\u5f84\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u6e90\u6587\u4ef6\u8def\u5f84\u548c\u76ee\u6807\u6587\u4ef6\u8def\u5f84\u90fd\u662f\u6b63\u786e\u7684\uff0c\u7279\u522b\u662f\u5728\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u65f6\uff0c\u9700\u8981\u786e\u8ba4\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u662f\u6b63\u786e\u7684\u3002<\/p>\n<\/p>\n<p><h4>5.2 \u6743\u9650\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5728\u590d\u5236\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u4e0d\u8db3\u7684\u95ee\u9898\u3002\u786e\u4fdd\u5bf9\u6e90\u6587\u4ef6\u5177\u6709\u8bfb\u53d6\u6743\u9650\uff0c\u5bf9\u76ee\u6807\u8def\u5f84\u5177\u6709\u5199\u5165\u6743\u9650\u3002<\/p>\n<\/p>\n<p><h4>5.3 \u9519\u8bef\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u590d\u5236\u64cd\u4f5c\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528try-except\u5757\u6355\u83b7\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\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>try:<\/p>\n<p>    shutil.copy(source_file, destination_file)<\/p>\n<p>    print(&quot;\u6587\u4ef6\u590d\u5236\u6210\u529f&quot;)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&quot;\u6e90\u6587\u4ef6\u4e0d\u5b58\u5728&quot;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(&quot;\u6743\u9650\u4e0d\u8db3&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;\u53d1\u751f\u9519\u8bef: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u4f7f\u7528Python\u8fdb\u884c\u6587\u4ef6\u590d\u5236\u64cd\u4f5c\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6587\u4ef6\u590d\u5236\uff0c\u8fd8\u662f\u9700\u8981\u4fdd\u7559\u5143\u6570\u636e\u6216\u8fdb\u884c\u989d\u5916\u7684\u6570\u636e\u5904\u7406\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u6587\u4ef6\u7684\u590d\u5236\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shutil<\/code>\u6a21\u5757\u6765\u590d\u5236\u6587\u4ef6\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u64cd\u4f5c\u3002\u8981\u590d\u5236\u6587\u4ef6\uff0c\u53ea\u9700\u4f7f\u7528<code>shutil.copy()<\/code>\u51fd\u6570\uff0c\u4f20\u5165\u6e90\u6587\u4ef6\u8def\u5f84\u548c\u76ee\u6807\u6587\u4ef6\u8def\u5f84\u5373\u53ef\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import shutil\n\nshutil.copy(&#39;source_file.txt&#39;, &#39;destination_file.txt&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06<code>source_file.txt<\/code>\u590d\u5236\u5230<code>destination_file.txt<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u6587\u4ef6\u590d\u5236\u65f6\u662f\u5426\u4f1a\u8986\u76d6\u76ee\u6807\u6587\u4ef6\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u5982\u679c\u76ee\u6807\u6587\u4ef6\u5df2\u7ecf\u5b58\u5728\uff0c\u4f7f\u7528<code>shutil.copy()<\/code>\u8fdb\u884c\u590d\u5236\u65f6\uff0c\u76ee\u6807\u6587\u4ef6\u5c06\u88ab\u8986\u76d6\u3002\u5982\u679c\u4e0d\u5e0c\u671b\u8986\u76d6\uff0c\u53ef\u4ee5\u5728\u590d\u5236\u4e4b\u524d\u68c0\u67e5\u76ee\u6807\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u4f7f\u7528<code>os.path.exists()<\/code>\u51fd\u6570\u6765\u5224\u65ad\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u590d\u5236\u6574\u4e2a\u76ee\u5f55\u53ca\u5176\u5185\u5bb9\uff1f<\/strong><br \/>\u8981\u590d\u5236\u6574\u4e2a\u76ee\u5f55\u53ca\u5176\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shutil.copytree()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u590d\u5236\u6307\u5b9a\u76ee\u5f55\u53ca\u5176\u6240\u6709\u5b50\u76ee\u5f55\u548c\u6587\u4ef6\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import shutil\n\nshutil.copytree(&#39;source_directory&#39;, &#39;destination_directory&#39;)\n<\/code><\/pre>\n<p>\u6267\u884c\u6b64\u4ee3\u7801\u540e\uff0c<code>source_directory<\/code>\u7684\u6240\u6709\u5185\u5bb9\u90fd\u4f1a\u88ab\u590d\u5236\u5230<code>destination_directory<\/code>\u4e2d\u3002\u786e\u4fdd\u76ee\u6807\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u590d\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528shutil\u6a21\u5757\u3001\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528pandas\u8bfb\u53d6\u5199\u5165\u3001\u4f7f\u7528path [&hellip;]","protected":false},"author":3,"featured_media":970195,"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\/970189"}],"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=970189"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/970189\/revisions"}],"predecessor-version":[{"id":970197,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/970189\/revisions\/970197"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/970195"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=970189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=970189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=970189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}