{"id":1158227,"date":"2025-01-13T18:38:08","date_gmt":"2025-01-13T10:38:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1158227.html"},"modified":"2025-01-13T18:38:10","modified_gmt":"2025-01-13T10:38:10","slug":"python%e5%a6%82%e4%bd%95%e6%89%b9%e9%87%8f%e5%a4%8d%e5%88%b6%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1158227.html","title":{"rendered":"Python\u5982\u4f55\u6279\u91cf\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\/25200239\/ee3b473e-fde9-421d-8897-ed1cf50c662d.webp\" alt=\"Python\u5982\u4f55\u6279\u91cf\u590d\u5236\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u6279\u91cf\u590d\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528shutil\u6a21\u5757\u3001os\u6a21\u5757\u548c\u8def\u5f84\u5e93pathlib\u6a21\u5757\uff0c\u63a8\u8350\u4f7f\u7528shutil\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u9ad8\u5c42\u6b21\u7684\u6587\u4ef6\u64cd\u4f5c\u63a5\u53e3\u3001\u7b80\u5355\u6613\u7528\u3001\u529f\u80fd\u5f3a\u5927\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a<strong>shutil\u6a21\u5757\u4e0d\u4ec5\u80fd\u5b8c\u6210\u6587\u4ef6\u7684\u590d\u5236\u64cd\u4f5c\uff0c\u8fd8\u80fd\u5904\u7406\u6587\u4ef6\u7684\u79fb\u52a8\u3001\u5220\u9664\u7b49\u591a\u79cd\u6587\u4ef6\u64cd\u4f5c\u3002\u5b83\u63d0\u4f9b\u7684<code>shutil.copy<\/code>\u548c<code>shutil.copy2<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6587\u4ef6\u4ece\u4e00\u4e2a\u76ee\u5f55\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u76ee\u5f55\u3002<code>shutil.copy<\/code>\u53ea\u590d\u5236\u6587\u4ef6\u5185\u5bb9\uff0c\u800c<code>shutil.copy2<\/code>\u8fd8\u80fd\u590d\u5236\u6587\u4ef6\u7684\u5143\u6570\u636e\u5982\u65f6\u95f4\u6233\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528shutil\u6a21\u5757\u6279\u91cf\u590d\u5236\u6587\u4ef6<\/p>\n<\/p>\n<p><p>shutil\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u4e13\u95e8\u7528\u4e8e\u9ad8\u7ea7\u6587\u4ef6\u64cd\u4f5c\u3002\u4f7f\u7528shutil\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6587\u4ef6\u590d\u5236\u3001\u79fb\u52a8\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528shutil\u6a21\u5757\u6279\u91cf\u590d\u5236\u6587\u4ef6\u7684\u5177\u4f53\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5355\u6587\u4ef6\u590d\u5236<\/strong><\/li>\n<\/ol>\n<p><p>\u8981\u590d\u5236\u5355\u4e2a\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shutil.copy<\/code>\u6216<code>shutil.copy2<\/code>\u51fd\u6570\u3002<code>shutil.copy<\/code>\u51fd\u6570\u53ea\u590d\u5236\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u800c<code>shutil.copy2<\/code>\u51fd\u6570\u5219\u4f1a\u8fde\u540c\u6587\u4ef6\u7684\u5143\u6570\u636e\uff08\u5982\u4fee\u6539\u65f6\u95f4\u3001\u6743\u9650\u7b49\uff09\u4e00\u8d77\u590d\u5236\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<h2><strong>\u6e90\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>src_file = &#39;path\/to\/source\/file.txt&#39;<\/p>\n<h2><strong>\u76ee\u6807\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>dst_file = &#39;path\/to\/destination\/file.txt&#39;<\/p>\n<h2><strong>\u590d\u5236\u6587\u4ef6<\/strong><\/h2>\n<p>shutil.copy(src_file, dst_file)<\/p>\n<h2><strong>\u6216\u8005\u4f7f\u7528shutil.copy2<\/strong><\/h2>\n<h2><strong>shutil.copy2(src_file, dst_file)<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6279\u91cf\u6587\u4ef6\u590d\u5236<\/strong><\/li>\n<\/ol>\n<p><p>\u5982\u679c\u8981\u6279\u91cf\u590d\u5236\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6e90\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff0c\u7136\u540e\u4f9d\u6b21\u8c03\u7528<code>shutil.copy<\/code>\u6216<code>shutil.copy2<\/code>\u51fd\u6570\u8fdb\u884c\u590d\u5236\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>import os<\/p>\n<h2><strong>\u6e90\u76ee\u5f55<\/strong><\/h2>\n<p>src_dir = &#39;path\/to\/source\/directory&#39;<\/p>\n<h2><strong>\u76ee\u6807\u76ee\u5f55<\/strong><\/h2>\n<p>dst_dir = &#39;path\/to\/destination\/directory&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u6e90\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6<\/strong><\/h2>\n<p>file_list = os.listdir(src_dir)<\/p>\n<h2><strong>\u904d\u5386\u5e76\u590d\u5236\u6587\u4ef6<\/strong><\/h2>\n<p>for file_name in file_list:<\/p>\n<p>    src_file = os.path.join(src_dir, file_name)<\/p>\n<p>    dst_file = os.path.join(dst_dir, file_name)<\/p>\n<p>    shutil.copy(src_file, dst_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u590d\u5236\u6574\u4e2a\u76ee\u5f55<\/strong><\/li>\n<\/ol>\n<p><p>\u5982\u679c\u8981\u590d\u5236\u6574\u4e2a\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shutil.copytree<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u4f1a\u9012\u5f52\u5730\u590d\u5236\u6e90\u76ee\u5f55\u53ca\u5176\u6240\u6709\u5185\u5bb9\u5230\u76ee\u6807\u76ee\u5f55\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<h2><strong>\u6e90\u76ee\u5f55<\/strong><\/h2>\n<p>src_dir = &#39;path\/to\/source\/directory&#39;<\/p>\n<h2><strong>\u76ee\u6807\u76ee\u5f55<\/strong><\/h2>\n<p>dst_dir = &#39;path\/to\/destination\/directory&#39;<\/p>\n<h2><strong>\u590d\u5236\u76ee\u5f55<\/strong><\/h2>\n<p>shutil.copytree(src_dir, dst_dir)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528os\u6a21\u5757\u6279\u91cf\u590d\u5236\u6587\u4ef6<\/p>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u53e6\u4e00\u4e2a\u6a21\u5757\uff0c\u4e13\u95e8\u7528\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7ea7\u522b\u7684\u64cd\u4f5c\u3002\u867d\u7136os\u6a21\u5757\u6ca1\u6709\u63d0\u4f9b\u76f4\u63a5\u7684\u6587\u4ef6\u590d\u5236\u51fd\u6570\uff0c\u4f46\u53ef\u4ee5\u7ed3\u5408os\u6a21\u5757\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u548cshutil\u6a21\u5757\u7684<code>shutil.copy<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u6279\u91cf\u590d\u5236\u6587\u4ef6\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5355\u6587\u4ef6\u590d\u5236<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<h2><strong>\u6e90\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>src_file = &#39;path\/to\/source\/file.txt&#39;<\/p>\n<h2><strong>\u76ee\u6807\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>dst_file = &#39;path\/to\/destination\/file.txt&#39;<\/p>\n<h2><strong>\u590d\u5236\u6587\u4ef6<\/strong><\/h2>\n<p>shutil.copy(src_file, dst_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6279\u91cf\u6587\u4ef6\u590d\u5236<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<h2><strong>\u6e90\u76ee\u5f55<\/strong><\/h2>\n<p>src_dir = &#39;path\/to\/source\/directory&#39;<\/p>\n<h2><strong>\u76ee\u6807\u76ee\u5f55<\/strong><\/h2>\n<p>dst_dir = &#39;path\/to\/destination\/directory&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u6e90\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6<\/strong><\/h2>\n<p>file_list = os.listdir(src_dir)<\/p>\n<h2><strong>\u904d\u5386\u5e76\u590d\u5236\u6587\u4ef6<\/strong><\/h2>\n<p>for file_name in file_list:<\/p>\n<p>    src_file = os.path.join(src_dir, file_name)<\/p>\n<p>    dst_file = os.path.join(dst_dir, file_name)<\/p>\n<p>    shutil.copy(src_file, dst_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528pathlib\u6a21\u5757\u6279\u91cf\u590d\u5236\u6587\u4ef6<\/p>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u7248\u672c\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u64cd\u4f5c\u63a5\u53e3\u3002\u4e0eos\u6a21\u5757\u76f8\u6bd4\uff0cpathlib\u6a21\u5757\u7684\u63a5\u53e3\u66f4\u52a0\u76f4\u89c2\u3001\u6613\u7528\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528pathlib\u6a21\u5757\u6279\u91cf\u590d\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5355\u6587\u4ef6\u590d\u5236<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>import shutil<\/p>\n<h2><strong>\u6e90\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>src_file = Path(&#39;path\/to\/source\/file.txt&#39;)<\/p>\n<h2><strong>\u76ee\u6807\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>dst_file = Path(&#39;path\/to\/destination\/file.txt&#39;)<\/p>\n<h2><strong>\u590d\u5236\u6587\u4ef6<\/strong><\/h2>\n<p>shutil.copy(src_file, dst_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6279\u91cf\u6587\u4ef6\u590d\u5236<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>import shutil<\/p>\n<h2><strong>\u6e90\u76ee\u5f55<\/strong><\/h2>\n<p>src_dir = Path(&#39;path\/to\/source\/directory&#39;)<\/p>\n<h2><strong>\u76ee\u6807\u76ee\u5f55<\/strong><\/h2>\n<p>dst_dir = Path(&#39;path\/to\/destination\/directory&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6e90\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6<\/strong><\/h2>\n<p>file_list = src_dir.iterdir()<\/p>\n<h2><strong>\u904d\u5386\u5e76\u590d\u5236\u6587\u4ef6<\/strong><\/h2>\n<p>for src_file in file_list:<\/p>\n<p>    dst_file = dst_dir \/ src_file.name<\/p>\n<p>    shutil.copy(src_file, dst_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6279\u91cf\u590d\u5236\u6587\u4ef6\u65f6\u7684\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u6279\u91cf\u590d\u5236\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u76ee\u6807\u76ee\u5f55\u7684\u5b58\u5728\u6027<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u590d\u5236\u6587\u4ef6\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u76ee\u6807\u76ee\u5f55\u5b58\u5728\u3002\u5982\u679c\u76ee\u6807\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u7684<code>os.makedirs<\/code>\u51fd\u6570\u6216pathlib\u6a21\u5757\u7684<code>Path.mkdir<\/code>\u65b9\u6cd5\u6765\u521b\u5efa\u76ee\u6807\u76ee\u5f55\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u76ee\u6807\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>dst_dir = &#39;path\/to\/destination\/directory&#39;<\/p>\n<h2><strong>\u521b\u5efa\u76ee\u6807\u76ee\u5f55<\/strong><\/h2>\n<p>os.makedirs(dst_dir, exist_ok=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u76ee\u6807\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>dst_dir = Path(&#39;path\/to\/destination\/directory&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u76ee\u6807\u76ee\u5f55<\/strong><\/h2>\n<p>dst_dir.mkdir(parents=True, exist_ok=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6587\u4ef6\u8986\u76d6<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u590d\u5236\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\u4f1a\u8986\u76d6\u76ee\u6807\u6587\u4ef6\u3002\u5982\u679c\u4e0d\u5e0c\u671b\u8986\u76d6\u76ee\u6807\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u590d\u5236\u6587\u4ef6\u4e4b\u524d\u68c0\u67e5\u76ee\u6807\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u5e76\u6839\u636e\u9700\u8981\u5904\u7406\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<h2><strong>\u6e90\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>src_file = &#39;path\/to\/source\/file.txt&#39;<\/p>\n<h2><strong>\u76ee\u6807\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>dst_file = &#39;path\/to\/destination\/file.txt&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u76ee\u6807\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if not os.path.exists(dst_file):<\/p>\n<p>    # \u590d\u5236\u6587\u4ef6<\/p>\n<p>    shutil.copy(src_file, dst_file)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;File {dst_file} already exists.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u6279\u91cf\u590d\u5236\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u4e3a\u4e86\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u9700\u8981\u8fdb\u884c\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6355\u83b7\u5e76\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<h2><strong>\u6e90\u76ee\u5f55<\/strong><\/h2>\n<p>src_dir = &#39;path\/to\/source\/directory&#39;<\/p>\n<h2><strong>\u76ee\u6807\u76ee\u5f55<\/strong><\/h2>\n<p>dst_dir = &#39;path\/to\/destination\/directory&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u6e90\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6<\/strong><\/h2>\n<p>file_list = os.listdir(src_dir)<\/p>\n<h2><strong>\u904d\u5386\u5e76\u590d\u5236\u6587\u4ef6<\/strong><\/h2>\n<p>for file_name in file_list:<\/p>\n<p>    src_file = os.path.join(src_dir, file_name)<\/p>\n<p>    dst_file = os.path.join(dst_dir, file_name)<\/p>\n<p>    try:<\/p>\n<p>        shutil.copy(src_file, dst_file)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Error copying {src_file} to {dst_file}: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u793a\u4f8b\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u4f7f\u7528shutil\u6a21\u5757\u6279\u91cf\u590d\u5236\u6587\u4ef6\u7684\u5b8c\u6574\u8fc7\u7a0b\uff0c\u5305\u62ec\u76ee\u6807\u76ee\u5f55\u7684\u521b\u5efa\u3001\u6587\u4ef6\u8986\u76d6\u68c0\u67e5\u548c\u9519\u8bef\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>from pathlib import Path<\/p>\n<p>def batch_copy_files(src_dir, dst_dir):<\/p>\n<p>    # \u521b\u5efa\u76ee\u6807\u76ee\u5f55<\/p>\n<p>    dst_dir = Path(dst_dir)<\/p>\n<p>    dst_dir.mkdir(parents=True, exist_ok=True)<\/p>\n<p>    # \u83b7\u53d6\u6e90\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6<\/p>\n<p>    src_dir = Path(src_dir)<\/p>\n<p>    file_list = src_dir.iterdir()<\/p>\n<p>    # \u904d\u5386\u5e76\u590d\u5236\u6587\u4ef6<\/p>\n<p>    for src_file in file_list:<\/p>\n<p>        dst_file = dst_dir \/ src_file.name<\/p>\n<p>        try:<\/p>\n<p>            if not dst_file.exists():<\/p>\n<p>                shutil.copy(src_file, dst_file)<\/p>\n<p>            else:<\/p>\n<p>                print(f&quot;File {dst_file} already exists.&quot;)<\/p>\n<p>        except Exception as e:<\/p>\n<p>            print(f&quot;Error copying {src_file} to {dst_file}: {e}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>src_dir = &#39;path\/to\/source\/directory&#39;<\/p>\n<p>dst_dir = &#39;path\/to\/destination\/directory&#39;<\/p>\n<p>batch_copy_files(src_dir, dst_dir)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u6279\u91cf\u590d\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4f7f\u7528shutil\u6a21\u5757\u3001os\u6a21\u5757\u548cpathlib\u6a21\u5757\u3002\u63a8\u8350\u4f7f\u7528shutil\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u9ad8\u5c42\u6b21\u7684\u6587\u4ef6\u64cd\u4f5c\u63a5\u53e3\uff0c\u7b80\u5355\u6613\u7528\uff0c\u529f\u80fd\u5f3a\u5927\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6ce8\u610f\u76ee\u6807\u76ee\u5f55\u7684\u5b58\u5728\u6027\u3001\u6587\u4ef6\u8986\u76d6\u548c\u9519\u8bef\u5904\u7406\u7b49\u95ee\u9898\u3002\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9009\u62e9\u591a\u4e2a\u6587\u4ef6\u8fdb\u884c\u6279\u91cf\u590d\u5236\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>tkinter<\/code>\u5e93\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u9009\u62e9\u5bf9\u8bdd\u6846\uff0c\u5141\u8bb8\u7528\u6237\u9009\u62e9\u591a\u4e2a\u6587\u4ef6\u8fdb\u884c\u590d\u5236\u3002\u7ed3\u5408<code>shutil<\/code>\u6a21\u5757\u7684<code>copy<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u9009\u62e9\u7684\u6587\u4ef6\u590d\u5236\u5230\u76ee\u6807\u76ee\u5f55\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>askopenfilenames()<\/code>\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u9009\u4e2d\u7684\u6587\u4ef6\u8def\u5f84\u5217\u8868\uff0c\u4e4b\u540e\u904d\u5386\u8fd9\u4e9b\u8def\u5f84\u5e76\u590d\u5236\u5230\u6307\u5b9a\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u6279\u91cf\u590d\u5236\u6587\u4ef6\u65f6\u5982\u4f55\u5904\u7406\u6587\u4ef6\u540d\u51b2\u7a81\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6279\u91cf\u590d\u5236\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6587\u4ef6\u540d\u51b2\u7a81\u7684\u60c5\u51b5\u3002\u5982\u679c\u76ee\u6807\u6587\u4ef6\u5939\u4e2d\u5df2\u7ecf\u5b58\u5728\u540c\u540d\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u590d\u5236\u4e4b\u524d\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u5e76\u6839\u636e\u9700\u8981\u4fee\u6539\u6587\u4ef6\u540d\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u6587\u4ef6\u540d\u540e\u6dfb\u52a0\u4e00\u4e2a\u6570\u5b57\u540e\u7f00\uff0c\u6216\u8005\u4f7f\u7528\u65f6\u95f4\u6233\u6765\u786e\u4fdd\u6587\u4ef6\u540d\u552f\u4e00\u3002\u4f7f\u7528<code>os.path.exists()<\/code>\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5b9e\u73b0\u7684\u6279\u91cf\u590d\u5236\u6587\u4ef6\u6027\u80fd\u5982\u4f55\u4f18\u5316\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8\u6279\u91cf\u590d\u5236\u6587\u4ef6\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65IO\u6765\u5e76\u884c\u5904\u7406\u6587\u4ef6\u590d\u5236\u4efb\u52a1\u3002<code>concurrent.futures<\/code>\u6a21\u5757\u7684<code>ThreadPoolExecutor<\/code>\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u591a\u7ebf\u7a0b\u590d\u5236\uff0c\u5145\u5206\u5229\u7528\u7cfb\u7edf\u8d44\u6e90\u3002\u540c\u65f6\uff0c\u4f7f\u7528<code>shutil.copy2()<\/code>\u800c\u975e<code>shutil.copy()<\/code>\u53ef\u4ee5\u4fdd\u7559\u6587\u4ef6\u7684\u5143\u6570\u636e\uff0c\u63d0\u9ad8\u590d\u5236\u7684\u5b8c\u6574\u6027\u3002\u6ce8\u610f\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u6709\u8db3\u591f\u7684\u5185\u5b58\u548c\u78c1\u76d8\u7a7a\u95f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u6279\u91cf\u590d\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528shutil\u6a21\u5757\u3001os\u6a21\u5757\u548c\u8def\u5f84\u5e93pathlib\u6a21\u5757\uff0c\u63a8\u8350\u4f7f\u7528shu [&hellip;]","protected":false},"author":3,"featured_media":1158238,"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\/1158227"}],"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=1158227"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1158227\/revisions"}],"predecessor-version":[{"id":1158239,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1158227\/revisions\/1158239"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1158238"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1158227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1158227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1158227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}