{"id":1050220,"date":"2024-12-31T14:06:00","date_gmt":"2024-12-31T06:06:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1050220.html"},"modified":"2024-12-31T14:06:03","modified_gmt":"2024-12-31T06:06:03","slug":"%e5%9c%a8python3%e4%b8%ad%e5%a6%82%e4%bd%95%e5%a4%8d%e5%88%b6%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1050220.html","title":{"rendered":"\u5728Python3\u4e2d\u5982\u4f55\u590d\u5236\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/067c768e-1dd7-44ec-9efd-c3810e2bc076.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5728Python3\u4e2d\u5982\u4f55\u590d\u5236\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Python3\u4e2d\u590d\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5982\u4f7f\u7528shutil\u6a21\u5757\u3001\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528pandas\u6a21\u5757\u7b49\u3002\u63a8\u8350\u4f7f\u7528shutil\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u6587\u4ef6\u590d\u5236\u64cd\u4f5c\u3002<\/strong> \u5176\u4e2d\uff0cshutil\u6a21\u5757\u7684<code>copy<\/code>\u548c<code>copy2<\/code>\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\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><h4>1\u3001shutil.copy<\/h4>\n<\/p>\n<p><p><code>shutil.copy<\/code> \u662f\u4e00\u4e2a\u7b80\u5355\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u590d\u5236\u6587\u4ef6\u3002\u5b83\u4e0d\u4ec5\u590d\u5236\u6587\u4ef6\u5185\u5bb9\uff0c\u8fd8\u590d\u5236\u6743\u9650\u4f4d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>source = &#39;source_file.txt&#39;<\/p>\n<p>destination = &#39;destination_file.txt&#39;<\/p>\n<p>shutil.copy(source, destination)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>source_file.txt<\/code> \u662f\u6e90\u6587\u4ef6\u8def\u5f84\uff0c<code>destination_file.txt<\/code> \u662f\u76ee\u6807\u6587\u4ef6\u8def\u5f84\u3002<code>shutil.copy<\/code> \u4f1a\u5c06\u6e90\u6587\u4ef6\u590d\u5236\u5230\u76ee\u6807\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2\u3001shutil.copy2<\/h4>\n<\/p>\n<p><p><code>shutil.copy2<\/code> \u9664\u4e86\u590d\u5236\u6587\u4ef6\u5185\u5bb9\u548c\u6743\u9650\u5916\uff0c\u8fd8\u590d\u5236\u6587\u4ef6\u7684\u5143\u6570\u636e\uff08\u5982\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u3001\u4fee\u6539\u65f6\u95f4\u7b49\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>source = &#39;source_file.txt&#39;<\/p>\n<p>destination = &#39;destination_file.txt&#39;<\/p>\n<p>shutil.copy2(source, destination)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 <code>shutil.copy2<\/code> \u53ef\u4ee5\u4fdd\u7559\u66f4\u591a\u7684\u6587\u4ef6\u5c5e\u6027\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528os\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>os<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4f4e\u7ea7\u522b\u7684\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5\uff0c\u9002\u5408\u4e8e\u66f4\u590d\u6742\u7684\u6587\u4ef6\u64cd\u4f5c\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\u590d\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>source = &#39;source_file.txt&#39;<\/p>\n<p>destination = &#39;destination_file.txt&#39;<\/p>\n<p>with open(source, &#39;rb&#39;) as src_file:<\/p>\n<p>    with open(destination, &#39;wb&#39;) as dst_file:<\/p>\n<p>        dst_file.write(src_file.read())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u590d\u5236\u9700\u6c42\uff0c\u4f46\u4e0d\u5982 <code>shutil<\/code> \u6a21\u5757\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pandas\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u8981\u590d\u5236\u7684\u662f\u6570\u636e\u6587\u4ef6\uff08\u5982csv\u6587\u4ef6\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>pandas<\/code> \u6a21\u5757\u6765\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>source = &#39;source_file.csv&#39;<\/p>\n<p>destination = &#39;destination_file.csv&#39;<\/p>\n<p>data = pd.read_csv(source)<\/p>\n<p>data.to_csv(destination, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6570\u636e\u6587\u4ef6\u7684\u590d\u5236\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u9884\u5904\u7406\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code> \u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u65b0\u7684\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002\u53ef\u4ee5\u7ed3\u5408 <code>shutil<\/code> \u6a21\u5757\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>import shutil<\/p>\n<p>source = Path(&#39;source_file.txt&#39;)<\/p>\n<p>destination = Path(&#39;destination_file.txt&#39;)<\/p>\n<p>shutil.copy(source, destination)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pathlib<\/code> \u6a21\u5757\u4f7f\u8def\u5f84\u64cd\u4f5c\u66f4\u52a0\u76f4\u89c2\u548c\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u6355\u83b7<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u590d\u5236\u64cd\u4f5c\u65f6\uff0c\u5e38\u5e38\u9700\u8981\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\u548c\u5f02\u5e38\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5305\u542b\u9519\u8bef\u5904\u7406\u7684\u6587\u4ef6\u590d\u5236\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>import os<\/p>\n<p>source = &#39;source_file.txt&#39;<\/p>\n<p>destination = &#39;destination_file.txt&#39;<\/p>\n<p>try:<\/p>\n<p>    if os.path.exists(source):<\/p>\n<p>        shutil.copy(source, destination)<\/p>\n<p>        print(f&#39;File copied successfully from {source} to {destination}&#39;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&#39;Source file {source} does not exist&#39;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(f&#39;Permission denied: unable to copy {source} to {destination}&#39;)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(f&#39;File not found: {source}&#39;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&#39;An error occurred: {e}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528 <code>try<\/code> \u548c <code>except<\/code> \u8bed\u53e5\u6355\u83b7\u5e76\u5904\u7406\u4e86\u6587\u4ef6\u590d\u5236\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u53d1\u751f\u7684\u5404\u79cd\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python3\u4e2d\u590d\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5176\u4e2d <code>shutil<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u6700\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<code>shutil.copy<\/code> \u548c <code>shutil.copy2<\/code> \u662f\u6700\u5e38\u7528\u7684\u6587\u4ef6\u590d\u5236\u65b9\u6cd5\uff0c\u6b64\u5916\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\u8fdb\u884c\u4f4e\u7ea7\u522b\u7684\u6587\u4ef6\u64cd\u4f5c\uff0c\u4f7f\u7528 <code>pandas<\/code> \u6a21\u5757\u590d\u5236\u6570\u636e\u6587\u4ef6\uff0c\u6216\u8005\u7ed3\u5408 <code>pathlib<\/code> \u6a21\u5757\u8fdb\u884c\u8def\u5f84\u64cd\u4f5c\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5e94\u6ce8\u610f\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\u548c\u5f02\u5e38\uff0c\u4ee5\u786e\u4fdd\u6587\u4ef6\u590d\u5236\u64cd\u4f5c\u7684\u53ef\u9760\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u9002\u5408\u7684\u6587\u4ef6\u590d\u5236\u65b9\u5f0f\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python3\u4e2d\uff0c\u590d\u5236\u6587\u4ef6\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u590d\u5236\u6587\u4ef6\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u4f7f\u7528<code>shutil<\/code>\u6a21\u5757\u4e2d\u7684<code>shutil.copy()<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u590d\u5236\u6587\u4ef6\u5185\u5bb9\uff0c\u8fd8\u53ef\u4ee5\u4fdd\u7559\u6587\u4ef6\u7684\u6743\u9650\u3002\u4f7f\u7528\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\u63d0\u4f9b\u7684\u8def\u5f84\u662f\u6709\u6548\u7684\uff0c\u5e76\u4e14\u76ee\u6807\u8def\u5f84\u6240\u5728\u7684\u76ee\u5f55\u5b58\u5728\uff0c\u4ee5\u907f\u514d\u9519\u8bef\u3002<\/p>\n<p><strong>\u4f7f\u7528Python3\u590d\u5236\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6587\u4ef6\u4e0d\u5b58\u5728\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u590d\u5236\u6587\u4ef6\u65f6\uff0c\u5982\u679c\u6e90\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u7a0b\u5e8f\u5c06\u629b\u51fa<code>FileNotFoundError<\/code>\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>os.path.exists()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u4ece\u800c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nimport shutil\n\nif os.path.exists(&#39;\u6e90\u6587\u4ef6\u8def\u5f84&#39;):\n    shutil.copy(&#39;\u6e90\u6587\u4ef6\u8def\u5f84&#39;, &#39;\u76ee\u6807\u6587\u4ef6\u8def\u5f84&#39;)\nelse:\n    print(&quot;\u6e90\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u8bf7\u68c0\u67e5\u8def\u5f84\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u5728\u6267\u884c\u590d\u5236\u64cd\u4f5c\u4e4b\u524d\u9a8c\u8bc1\u6587\u4ef6\u7684\u5b58\u5728\u6027\u3002<\/p>\n<p><strong>\u5728Python3\u4e2d\uff0c\u590d\u5236\u6587\u4ef6\u65f6\u5982\u4f55\u4fdd\u6301\u6587\u4ef6\u7684\u5143\u6570\u636e\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5728\u590d\u5236\u6587\u4ef6\u65f6\u4fdd\u7559\u66f4\u591a\u7684\u5143\u6570\u636e\uff08\u4f8b\u5982\u65f6\u95f4\u6233\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shutil.copy2()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u4e0d\u4ec5\u4f1a\u590d\u5236\u6587\u4ef6\u5185\u5bb9\uff0c\u8fd8\u4f1a\u590d\u5236\u6587\u4ef6\u7684\u6240\u6709\u5143\u6570\u636e\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import shutil\n\nshutil.copy2(&#39;\u6e90\u6587\u4ef6\u8def\u5f84&#39;, &#39;\u76ee\u6807\u6587\u4ef6\u8def\u5f84&#39;)\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>shutil.copy2()<\/code>\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4\u548c\u4fee\u6539\u65f6\u95f4\u7b49\u4fe1\u606f\u4e5f\u88ab\u4fdd\u7559\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python3\u4e2d\u590d\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5982\u4f7f\u7528shutil\u6a21\u5757\u3001\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528pandas\u6a21\u5757\u7b49\u3002\u63a8\u8350\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1050234,"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\/1050220"}],"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=1050220"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1050220\/revisions"}],"predecessor-version":[{"id":1050237,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1050220\/revisions\/1050237"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1050234"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1050220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1050220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1050220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}