{"id":1149359,"date":"2025-01-13T16:48:35","date_gmt":"2025-01-13T08:48:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149359.html"},"modified":"2025-01-13T16:48:38","modified_gmt":"2025-01-13T08:48:38","slug":"python%e5%a6%82%e4%bd%95%e5%8f%af%e4%bb%a5%e5%a4%8d%e5%88%b6%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1149359.html","title":{"rendered":"python\u5982\u4f55\u53ef\u4ee5\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\/25180030\/8f44df34-f61a-49de-9039-7e2b5d618367.webp\" alt=\"python\u5982\u4f55\u53ef\u4ee5\u590d\u5236\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u590d\u5236\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528shutil\u6a21\u5757\u3001os\u6a21\u5757\u6216\u76f4\u63a5\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5<\/strong>\uff0c\u5176\u4e2d<strong>shutil\u6a21\u5757<\/strong>\u662f\u6700\u5e38\u7528\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u91cd\u70b9\u4ecb\u7ecdshutil\u6a21\u5757\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001shutil\u6a21\u5757<\/h3>\n<\/p>\n<p><p>shutil\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u9ad8\u7ea7\u6587\u4ef6\u64cd\u4f5c\u6a21\u5757\uff0c\u4e13\u95e8\u7528\u4e8e\u6267\u884c\u6587\u4ef6\u548c\u6587\u4ef6\u96c6\u5408\u7684\u9ad8\u7ea7\u64cd\u4f5c\u3002\u5b83\u63d0\u4f9b\u4e86<code>shutil.copy()<\/code>\u3001<code>shutil.copy2()<\/code>\u3001<code>shutil.copyfile()<\/code>\u7b49\u65b9\u6cd5\u6765\u590d\u5236\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1. shutil.copy()<\/h4>\n<\/p>\n<p><p><code>shutil.copy(src, dst)<\/code> \u65b9\u6cd5\u5c06\u6587\u4ef6\u5185\u5bb9\u548c\u6743\u9650\u590d\u5236\u5230\u76ee\u6807\u6587\u4ef6\u6216\u76ee\u5f55\u4e2d\u3002\u5982\u679c\u76ee\u6807\u662f\u76ee\u5f55\uff0c\u5219\u6e90\u6587\u4ef6\u4f1a\u590d\u5236\u5230\u8be5\u76ee\u5f55\u4e2d\u5e76\u4fdd\u7559\u539f\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>src_file = &#39;path\/to\/source\/file.txt&#39;<\/p>\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<p><h4>2. shutil.copy2()<\/h4>\n<\/p>\n<p><p><code>shutil.copy2(src, dst)<\/code> \u65b9\u6cd5\u4e0e <code>shutil.copy()<\/code> \u7c7b\u4f3c\uff0c\u4f46\u5b83\u8fd8\u4f1a\u5c1d\u8bd5\u4fdd\u7559\u6e90\u6587\u4ef6\u7684\u5143\u6570\u636e\uff08\u5982\u4fee\u6539\u65f6\u95f4\u3001\u8bbf\u95ee\u65f6\u95f4\u7b49\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>src_file = &#39;path\/to\/source\/file.txt&#39;<\/p>\n<p>dst_file = &#39;path\/to\/destination\/file.txt&#39;<\/p>\n<h2><strong>\u590d\u5236\u6587\u4ef6\u5e76\u4fdd\u7559\u5143\u6570\u636e<\/strong><\/h2>\n<p>shutil.copy2(src_file, dst_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. shutil.copyfile()<\/h4>\n<\/p>\n<p><p><code>shutil.copyfile(src, dst)<\/code> \u65b9\u6cd5\u53ea\u590d\u5236\u6587\u4ef6\u5185\u5bb9\uff0c\u4e0d\u590d\u5236\u6587\u4ef6\u6743\u9650\u548c\u5143\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>src_file = &#39;path\/to\/source\/file.txt&#39;<\/p>\n<p>dst_file = &#39;path\/to\/destination\/file.txt&#39;<\/p>\n<h2><strong>\u4ec5\u590d\u5236\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>shutil.copyfile(src_file, dst_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001os\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u867d\u7136os\u6a21\u5757\u6ca1\u6709\u4e13\u95e8\u7684\u6587\u4ef6\u590d\u5236\u51fd\u6570\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5\u6765\u5b9e\u73b0\u6587\u4ef6\u590d\u5236\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8bfb\u53d6\u6e90\u6587\u4ef6\u5185\u5bb9\u5e76\u5199\u5165\u76ee\u6807\u6587\u4ef6\u6765\u5b9e\u73b0\u6587\u4ef6\u590d\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>src_file = &#39;path\/to\/source\/file.txt&#39;<\/p>\n<p>dst_file = &#39;path\/to\/destination\/file.txt&#39;<\/p>\n<h2><strong>\u8bfb\u53d6\u6e90\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>with open(src_file, &#39;rb&#39;) as f_src:<\/p>\n<p>    content = f_src.read()<\/p>\n<h2><strong>\u5c06\u5185\u5bb9\u5199\u5165\u76ee\u6807\u6587\u4ef6<\/strong><\/h2>\n<p>with open(dst_file, &#39;wb&#39;) as f_dst:<\/p>\n<p>    f_dst.write(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528os.popen()\u548c\u7cfb\u7edf\u547d\u4ee4<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u6765\u590d\u5236\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>src_file = &#39;path\/to\/source\/file.txt&#39;<\/p>\n<p>dst_file = &#39;path\/to\/destination\/file.txt&#39;<\/p>\n<h2><strong>\u4f7f\u7528\u7cfb\u7edf\u547d\u4ee4\u590d\u5236\u6587\u4ef6<\/strong><\/h2>\n<p>os.popen(f&#39;cp {src_file} {dst_file}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001Pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Pathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u5904\u7406\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7ed3\u5408\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5\u6765\u5b9e\u73b0\u6587\u4ef6\u590d\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>src_file = Path(&#39;path\/to\/source\/file.txt&#39;)<\/p>\n<p>dst_file = Path(&#39;path\/to\/destination\/file.txt&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6e90\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>content = src_file.read_bytes()<\/p>\n<h2><strong>\u5c06\u5185\u5bb9\u5199\u5165\u76ee\u6807\u6587\u4ef6<\/strong><\/h2>\n<p>dst_file.write_bytes(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Python\u6807\u51c6\u5e93\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>send2trash<\/code>\u6765\u5b9e\u73b0\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from send2trash import send2trash<\/p>\n<p>src_file = &#39;path\/to\/source\/file.txt&#39;<\/p>\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>\u5c06\u6e90\u6587\u4ef6\u79fb\u52a8\u5230\u5783\u573e\u7bb1<\/strong><\/h2>\n<p>send2trash(src_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>Python\u590d\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4f7f\u7528shutil\u6a21\u5757\u3001os\u6a21\u5757\u548cpathlib\u6a21\u5757<\/strong>\uff0c\u5176\u4e2dshutil\u6a21\u5757\u662f\u6700\u5e38\u7528\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u6587\u4ef6\u590d\u5236\u65b9\u5f0f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528shutil\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u80fd\u591f\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u6587\u4ef6\u590d\u5236\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6587\u4ef6\u590d\u5236\u64cd\u4f5c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u95ee\u9898\u548c\u6311\u6218\uff0c\u5982\u6587\u4ef6\u6743\u9650\u3001\u6587\u4ef6\u9501\u5b9a\u3001\u76ee\u6807\u8def\u5f84\u4e0d\u5b58\u5728\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879\u548c\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1. \u6587\u4ef6\u6743\u9650<\/h4>\n<\/p>\n<p><p>\u5728\u590d\u5236\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u95ee\u9898\uff0c\u5bfc\u81f4\u6587\u4ef6\u65e0\u6cd5\u8bfb\u53d6\u6216\u5199\u5165\u3002\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u6587\u4ef6\u6743\u9650\u5e76\u9002\u5f53\u4fee\u6539\u6765\u89e3\u51b3\u6743\u9650\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>src_file = &#39;path\/to\/source\/file.txt&#39;<\/p>\n<p>dst_file = &#39;path\/to\/destination\/file.txt&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u6e90\u6587\u4ef6\u6743\u9650<\/strong><\/h2>\n<p>if os.access(src_file, os.R_OK):<\/p>\n<p>    with open(src_file, &#39;rb&#39;) as f_src:<\/p>\n<p>        content = f_src.read()<\/p>\n<p>    # \u68c0\u67e5\u76ee\u6807\u8def\u5f84\u6743\u9650<\/p>\n<p>    dst_dir = os.path.dirname(dst_file)<\/p>\n<p>    if os.access(dst_dir, os.W_OK):<\/p>\n<p>        with open(dst_file, &#39;wb&#39;) as f_dst:<\/p>\n<p>            f_dst.write(content)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;\u76ee\u6807\u8def\u5f84 {dst_dir} \u65e0\u5199\u6743\u9650&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;\u6e90\u6587\u4ef6 {src_file} \u65e0\u8bfb\u53d6\u6743\u9650&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6587\u4ef6\u9501\u5b9a<\/h4>\n<\/p>\n<p><p>\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6587\u4ef6\u53ef\u80fd\u88ab\u5176\u4ed6\u8fdb\u7a0b\u9501\u5b9a\uff0c\u5bfc\u81f4\u65e0\u6cd5\u590d\u5236\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u7ed3\u6784\u6355\u83b7\u5f02\u5e38\u5e76\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>src_file = &#39;path\/to\/source\/file.txt&#39;<\/p>\n<p>dst_file = &#39;path\/to\/destination\/file.txt&#39;<\/p>\n<p>try:<\/p>\n<p>    shutil.copy(src_file, dst_file)<\/p>\n<p>except PermissionError as e:<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u88ab\u9501\u5b9a\uff0c\u65e0\u6cd5\u590d\u5236: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u76ee\u6807\u8def\u5f84\u4e0d\u5b58\u5728<\/h4>\n<\/p>\n<p><p>\u5728\u590d\u5236\u6587\u4ef6\u65f6\uff0c\u76ee\u6807\u8def\u5f84\u53ef\u80fd\u4e0d\u5b58\u5728\uff0c\u9700\u8981\u63d0\u524d\u68c0\u67e5\u5e76\u521b\u5efa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>src_file = &#39;path\/to\/source\/file.txt&#39;<\/p>\n<p>dst_file = &#39;path\/to\/destination\/file.txt&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u76ee\u6807\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>dst_dir = os.path.dirname(dst_file)<\/p>\n<p>if not os.path.exists(dst_dir):<\/p>\n<p>    os.makedirs(dst_dir)<\/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<p><h4>4. \u5927\u6587\u4ef6\u590d\u5236<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6587\u4ef6\u7684\u590d\u5236\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5206\u5757\u8bfb\u53d6\u548c\u5199\u5165\u7684\u65b9\u6cd5\uff0c\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def copy_large_file(src, dst, buffer_size=1024*1024):<\/p>\n<p>    with open(src, &#39;rb&#39;) as f_src, open(dst, &#39;wb&#39;) as f_dst:<\/p>\n<p>        while True:<\/p>\n<p>            chunk = f_src.read(buffer_size)<\/p>\n<p>            if not chunk:<\/p>\n<p>                break<\/p>\n<p>            f_dst.write(chunk)<\/p>\n<p>src_file = &#39;path\/to\/large\/source\/file.txt&#39;<\/p>\n<p>dst_file = &#39;path\/to\/large\/destination\/file.txt&#39;<\/p>\n<h2><strong>\u590d\u5236\u5927\u6587\u4ef6<\/strong><\/h2>\n<p>copy_large_file(src_file, dst_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u6216\u5927\u6587\u4ef6\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u6765\u4f18\u5316\u6587\u4ef6\u590d\u5236\u7684\u6027\u80fd\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5e76\u884c\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5b9e\u73b0\u5e76\u884c\u590d\u5236\u591a\u4e2a\u6587\u4ef6\uff0c\u4ee5\u63d0\u9ad8\u6574\u4f53\u590d\u5236\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def copy_file(src_dst):<\/p>\n<p>    src, dst = src_dst<\/p>\n<p>    shutil.copy(src, dst)<\/p>\n<p>src_files = [&#39;path\/to\/source\/file1.txt&#39;, &#39;path\/to\/source\/file2.txt&#39;]<\/p>\n<p>dst_files = [&#39;path\/to\/destination\/file1.txt&#39;, &#39;path\/to\/destination\/file2.txt&#39;]<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b\u6c60<\/strong><\/h2>\n<p>with ThreadPoolExecutor(max_workers=4) as executor:<\/p>\n<p>    executor.map(copy_file, zip(src_files, dst_files))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u6587\u4ef6\u590d\u5236\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u9ad8\u6548\u6587\u4ef6\u590d\u5236\u5de5\u5177\u53ef\u80fd\u4f1a\u6bd4\u7eafPython\u5b9e\u73b0\u66f4\u5feb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def copy_file_with_tool(src, dst):<\/p>\n<p>    os.system(f&#39;cp {src} {dst}&#39;)<\/p>\n<p>src_files = [&#39;path\/to\/source\/file1.txt&#39;, &#39;path\/to\/source\/file2.txt&#39;]<\/p>\n<p>dst_files = [&#39;path\/to\/destination\/file1.txt&#39;, &#39;path\/to\/destination\/file2.txt&#39;]<\/p>\n<p>for src, dst in zip(src_files, dst_files):<\/p>\n<p>    copy_file_with_tool(src, dst)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\u662f\u4fdd\u8bc1\u7a0b\u5e8f\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u7684\u5173\u952e\u3002\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u548c\u8bb0\u5f55\u65e5\u5fd7\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/h2>\n<p>logging.basicConfig(filename=&#39;file_copy.log&#39;, level=logging.INFO)<\/p>\n<p>def copy_file(src, dst):<\/p>\n<p>    try:<\/p>\n<p>        shutil.copy(src, dst)<\/p>\n<p>        logging.info(f&#39;\u6587\u4ef6\u590d\u5236\u6210\u529f: {src} -&gt; {dst}&#39;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        logging.error(f&#39;\u6587\u4ef6\u590d\u5236\u5931\u8d25: {src} -&gt; {dst}, \u9519\u8bef: {e}&#39;)<\/p>\n<p>src_files = [&#39;path\/to\/source\/file1.txt&#39;, &#39;path\/to\/source\/file2.txt&#39;]<\/p>\n<p>dst_files = [&#39;path\/to\/destination\/file1.txt&#39;, &#39;path\/to\/destination\/file2.txt&#39;]<\/p>\n<p>for src, dst in zip(src_files, dst_files):<\/p>\n<p>    copy_file(src, dst)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u6587\u4ef6\u590d\u5236\u64cd\u4f5c\uff0c\u5176\u4e2d<strong>shutil\u6a21\u5757\u662f\u6700\u5e38\u7528\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5<\/strong>\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u6587\u4ef6\u590d\u5236\u65b9\u5f0f\uff0c\u5e76\u6ce8\u610f\u5904\u7406\u6587\u4ef6\u6743\u9650\u3001\u6587\u4ef6\u9501\u5b9a\u3001\u76ee\u6807\u8def\u5f84\u4e0d\u5b58\u5728\u7b49\u95ee\u9898\u3002\u540c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5e76\u884c\u5904\u7406\u3001\u4f7f\u7528\u9ad8\u6548\u5de5\u5177\u3001\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\u7b49\u65b9\u6cd5\u6765\u4f18\u5316\u6027\u80fd\u548c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\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\u57fa\u672c\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>shutil<\/code>\u6a21\u5757\u6765\u590d\u5236\u6587\u4ef6\u3002<code>shutil.copy()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u63a5\u53d7\u6e90\u6587\u4ef6\u8def\u5f84\u548c\u76ee\u6807\u6587\u4ef6\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u5c06\u6e90\u6587\u4ef6\u590d\u5236\u5230\u6307\u5b9a\u4f4d\u7f6e\u3002\u793a\u4f8b\u4ee3\u7801\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\u4e2a\u793a\u4f8b\u5c06<code>source_file.txt<\/code>\u590d\u5236\u5230<code>destination_file.txt<\/code>\uff0c\u5982\u679c\u76ee\u6807\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5219\u5c06\u5176\u8986\u76d6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u590d\u5236\u6574\u4e2a\u76ee\u5f55\u53ca\u5176\u5185\u5bb9\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u590d\u5236\u4e00\u4e2a\u76ee\u5f55\u53ca\u5176\u6240\u6709\u5b50\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shutil.copytree()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u5c06\u6574\u4e2a\u76ee\u5f55\u6811\u590d\u5236\u5230\u6307\u5b9a\u7684\u76ee\u6807\u4f4d\u7f6e\u3002\u793a\u4f8b\u4ee3\u7801\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>\u8fd9\u4e2a\u793a\u4f8b\u5c06<code>source_directory<\/code>\u53ca\u5176\u6240\u6709\u5185\u5bb9\u590d\u5236\u5230<code>destination_directory<\/code>\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u590d\u5236\u6587\u4ef6\u65f6\uff0c\u662f\u5426\u53ef\u4ee5\u9009\u62e9\u53ea\u590d\u5236\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u7ed3\u5408<code>os<\/code>\u6a21\u5757\u548c<code>shutil<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u53ea\u590d\u5236\u7279\u5b9a\u7c7b\u578b\u6587\u4ef6\u7684\u9700\u6c42\u3002\u4f7f\u7528<code>os.listdir()<\/code>\u51fd\u6570\u5217\u51fa\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u6765\u9009\u62e9\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\u8fdb\u884c\u590d\u5236\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import os\nimport shutil\n\nsource_dir = &#39;source_directory&#39;\ndestination_dir = &#39;destination_directory&#39;\nfile_extension = &#39;.txt&#39;\n\nfor filename in os.listdir(source_dir):\n    if filename.endswith(file_extension):\n        shutil.copy(os.path.join(source_dir, filename), destination_dir)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u5c06<code>source_directory<\/code>\u4e2d\u6240\u6709\u6269\u5c55\u540d\u4e3a<code>.txt<\/code>\u7684\u6587\u4ef6\u590d\u5236\u5230<code>destination_directory<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u590d\u5236\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528shutil\u6a21\u5757\u3001os\u6a21\u5757\u6216\u76f4\u63a5\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5176\u4e2dshutil\u6a21\u5757\u662f\u6700\u5e38\u7528\u548c\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1149364,"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\/1149359"}],"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=1149359"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149359\/revisions"}],"predecessor-version":[{"id":1149367,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149359\/revisions\/1149367"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1149364"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1149359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1149359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1149359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}