{"id":1118191,"date":"2025-01-08T18:33:09","date_gmt":"2025-01-08T10:33:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1118191.html"},"modified":"2025-01-08T18:33:24","modified_gmt":"2025-01-08T10:33:24","slug":"python-%e5%a6%82%e4%bd%95%e5%a4%8d%e5%88%b6%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e5%a4%b9-3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1118191.html","title":{"rendered":"python \u5982\u4f55\u590d\u5236\u4e00\u4e2a\u6587\u4ef6\u5939"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081752\/6a3d6d42-2d65-4c83-a7b8-52109eb3ed76.webp\" alt=\"python \u5982\u4f55\u590d\u5236\u4e00\u4e2a\u6587\u4ef6\u5939\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u590d\u5236\u4e00\u4e2a\u6587\u4ef6\u5939\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528shutil\u6a21\u5757\u3001os\u6a21\u5757\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u5982 distutils\u7b49\u3002\u6700\u5e38\u7528\u548c\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528shutil\u6a21\u5757\u3002<\/strong> \u5176\u4e2d\uff0cshutil.copytree()\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\uff0c\u53ef\u4ee5\u9012\u5f52\u5730\u590d\u5236\u6574\u4e2a\u6587\u4ef6\u5939\u53ca\u5176\u5305\u542b\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528shutil\u6a21\u5757\u590d\u5236\u4e00\u4e2a\u6587\u4ef6\u5939\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\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u5305\u62ec\u590d\u5236\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002\u6211\u4eec\u4e3b\u8981\u4f7f\u7528shutil.copytree()\u51fd\u6570\u6765\u590d\u5236\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528shutil.copytree()\u590d\u5236\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>shutil.copytree()\u51fd\u6570\u53ef\u4ee5\u9012\u5f52\u5730\u590d\u5236\u6574\u4e2a\u6587\u4ef6\u5939\uff0c\u5305\u62ec\u5176\u6240\u6709\u5185\u5bb9\u3002\u5b83\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>source = &#39;path\/to\/source\/folder&#39;<\/p>\n<p>destination = &#39;path\/to\/destination\/folder&#39;<\/p>\n<p>shutil.copytree(source, destination)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0csource\u662f\u4f60\u60f3\u8981\u590d\u5236\u7684\u6e90\u6587\u4ef6\u5939\u7684\u8def\u5f84\uff0cdestination\u662f\u76ee\u6807\u6587\u4ef6\u5939\u7684\u8def\u5f84\u3002\u5982\u679c\u76ee\u6807\u6587\u4ef6\u5939\u4e0d\u5b58\u5728\uff0cshutil.copytree()\u4f1a\u81ea\u52a8\u521b\u5efa\u5b83\u3002<\/p>\n<\/p>\n<p><h4>2. \u5904\u7406\u5df2\u5b58\u5728\u7684\u76ee\u6807\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u76ee\u6807\u6587\u4ef6\u5939\u5df2\u7ecf\u5b58\u5728\uff0cshutil.copytree()\u4f1a\u629b\u51fa\u4e00\u4e2aFileExistsError\u5f02\u5e38\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u5728\u590d\u5236\u4e4b\u524d\u5148\u5220\u9664\u76ee\u6807\u6587\u4ef6\u5939\uff0c\u6216\u8005\u4f7f\u7528\u4e00\u4e2a\u4e0d\u540c\u7684\u76ee\u6807\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>import os<\/p>\n<p>source = &#39;path\/to\/source\/folder&#39;<\/p>\n<p>destination = &#39;path\/to\/destination\/folder&#39;<\/p>\n<p>if os.path.exists(destination):<\/p>\n<p>    shutil.rmtree(destination)<\/p>\n<p>shutil.copytree(source, destination)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u590d\u5236\u4e4b\u524d\u68c0\u67e5\u76ee\u6807\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u5b58\u5728\u5219\u5220\u9664\u5b83\u3002<\/p>\n<\/p>\n<p><h4>3. \u590d\u5236\u7279\u5b9a\u6587\u4ef6\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u53ea\u60f3\u590d\u5236\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u800c\u4e0d\u662f\u6574\u4e2a\u6587\u4ef6\u5939\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528shutil.copy2()\u51fd\u6570\u548cos.walk()\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>import os<\/p>\n<p>source = &#39;path\/to\/source\/folder&#39;<\/p>\n<p>destination = &#39;path\/to\/destination\/folder&#39;<\/p>\n<p>file_types = (&#39;.txt&#39;, &#39;.jpg&#39;)<\/p>\n<p>if not os.path.exists(destination):<\/p>\n<p>    os.makedirs(destination)<\/p>\n<p>for root, dirs, files in os.walk(source):<\/p>\n<p>    for file in files:<\/p>\n<p>        if file.endswith(file_types):<\/p>\n<p>            source_file = os.path.join(root, file)<\/p>\n<p>            destination_file = os.path.join(destination, os.path.relpath(root, source), file)<\/p>\n<p>            destination_dir = os.path.dirname(destination_file)<\/p>\n<p>            if not os.path.exists(destination_dir):<\/p>\n<p>                os.makedirs(destination_dir)<\/p>\n<p>            shutil.copy2(source_file, destination_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528os.walk()\u904d\u5386\u6e90\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff0c\u68c0\u67e5\u6bcf\u4e2a\u6587\u4ef6\u7684\u6269\u5c55\u540d\u662f\u5426\u5728\u6211\u4eec\u6307\u5b9a\u7684file_types\u5143\u7ec4\u4e2d\u3002\u5982\u679c\u662f\uff0c\u6211\u4eec\u4f7f\u7528shutil.copy2()\u51fd\u6570\u5c06\u5b83\u590d\u5236\u5230\u76ee\u6807\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001os\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1shutil\u6a21\u5757\u662f\u590d\u5236\u6587\u4ef6\u5939\u7684\u9996\u9009\u65b9\u6cd5\uff0c\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u60f3\u4f7f\u7528os\u6a21\u5757\u6765\u5b9e\u73b0\u66f4\u591a\u7684\u63a7\u5236\u548c\u5b9a\u5236\u3002os\u6a21\u5757\u63d0\u4f9b\u4e86\u4f4e\u7ea7\u522b\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u624b\u52a8\u590d\u5236\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528os\u6a21\u5757\u590d\u5236\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528os.makedirs()\u51fd\u6570\u521b\u5efa\u76ee\u6807\u6587\u4ef6\u5939\uff0c\u5e76\u4f7f\u7528shutil.copy2()\u51fd\u6570\u590d\u5236\u6bcf\u4e2a\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>source = &#39;path\/to\/source\/folder&#39;<\/p>\n<p>destination = &#39;path\/to\/destination\/folder&#39;<\/p>\n<p>for root, dirs, files in os.walk(source):<\/p>\n<p>    for dir in dirs:<\/p>\n<p>        os.makedirs(os.path.join(destination, os.path.relpath(os.path.join(root, dir), source)), exist_ok=True)<\/p>\n<p>    for file in files:<\/p>\n<p>        shutil.copy2(os.path.join(root, file), os.path.join(destination, os.path.relpath(root, source), file))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528os.walk()\u904d\u5386\u6e90\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\uff0c\u5e76\u624b\u52a8\u521b\u5efa\u76ee\u6807\u6587\u4ef6\u5939\u7684\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u867d\u7136shutil\u6a21\u5757\u548cos\u6a21\u5757\u5df2\u7ecf\u8db3\u591f\u5f3a\u5927\uff0c\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6765\u5b9e\u73b0\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\u3002distutils\u6a21\u5757\u662f\u4e00\u4e2a\u8fd9\u6837\u7684\u5e93\uff0c\u5c3d\u7ba1\u5b83\u5728\u590d\u5236\u6587\u4ef6\u5939\u65b9\u9762\u5e76\u4e0d\u5e38\u7528\uff0c\u4f46\u4f60\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b9e\u73b0\u590d\u5236\u6587\u4ef6\u5939\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528distutils.dir_util.copy_tree()\u590d\u5236\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>distutils.dir_util.copy_tree()\u51fd\u6570\u53ef\u4ee5\u9012\u5f52\u5730\u590d\u5236\u6574\u4e2a\u6587\u4ef6\u5939\uff0c\u5305\u62ec\u5176\u6240\u6709\u5185\u5bb9\u3002\u5b83\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from distutils.dir_util import copy_tree<\/p>\n<p>source = &#39;path\/to\/source\/folder&#39;<\/p>\n<p>destination = &#39;path\/to\/destination\/folder&#39;<\/p>\n<p>copy_tree(source, destination)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0csource\u662f\u4f60\u60f3\u8981\u590d\u5236\u7684\u6e90\u6587\u4ef6\u5939\u7684\u8def\u5f84\uff0cdestination\u662f\u76ee\u6807\u6587\u4ef6\u5939\u7684\u8def\u5f84\u3002\u5982\u679c\u76ee\u6807\u6587\u4ef6\u5939\u4e0d\u5b58\u5728\uff0ccopy_tree()\u4f1a\u81ea\u52a8\u521b\u5efa\u5b83\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u6587\u4ef6\u6743\u9650<\/h3>\n<\/p>\n<p><p>\u5728\u590d\u5236\u6587\u4ef6\u5939\u65f6\uff0c\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u4f1a\u9047\u5230\u6587\u4ef6\u6743\u9650\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6e90\u6587\u4ef6\u5939\u4e2d\u7684\u67d0\u4e9b\u6587\u4ef6\u662f\u53ea\u8bfb\u7684\uff0cshutil.copytree()\u51fd\u6570\u53ef\u80fd\u4f1a\u629b\u51fa\u4e00\u4e2aPermissionError\u5f02\u5e38\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u5728\u590d\u5236\u4e4b\u524d\u624b\u52a8\u4fee\u6539\u6587\u4ef6\u6743\u9650\uff0c\u6216\u8005\u5728\u590d\u5236\u4e4b\u540e\u6062\u590d\u539f\u59cb\u6743\u9650\u3002<\/p>\n<\/p>\n<p><h4>1. \u4fee\u6539\u6587\u4ef6\u6743\u9650<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528os.chmod()\u51fd\u6570\u4fee\u6539\u6587\u4ef6\u6743\u9650\uff0c\u4ee5\u786e\u4fdd\u5728\u590d\u5236\u8fc7\u7a0b\u4e2d\u4e0d\u4f1a\u9047\u5230\u6743\u9650\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>import stat<\/p>\n<p>source = &#39;path\/to\/source\/folder&#39;<\/p>\n<p>destination = &#39;path\/to\/destination\/folder&#39;<\/p>\n<p>def make_writable(path):<\/p>\n<p>    os.chmod(path, stat.S_IWUSR)<\/p>\n<p>for root, dirs, files in os.walk(source):<\/p>\n<p>    for file in files:<\/p>\n<p>        make_writable(os.path.join(root, file))<\/p>\n<p>shutil.copytree(source, destination)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2amake_writable()\u51fd\u6570\uff0c\u7528\u6765\u5c06\u6587\u4ef6\u6743\u9650\u4fee\u6539\u4e3a\u53ef\u5199\u3002\u7136\u540e\uff0c\u6211\u4eec\u5728\u590d\u5236\u4e4b\u524d\u904d\u5386\u6e90\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff0c\u5e76\u5c06\u5b83\u4eec\u7684\u6743\u9650\u4fee\u6539\u4e3a\u53ef\u5199\u3002<\/p>\n<\/p>\n<p><h4>2. \u6062\u590d\u6587\u4ef6\u6743\u9650<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5728\u590d\u5236\u4e4b\u540e\u6062\u590d\u539f\u59cb\u6743\u9650\uff0c\u4f60\u53ef\u4ee5\u5728\u590d\u5236\u4e4b\u524d\u8bb0\u5f55\u6587\u4ef6\u7684\u6743\u9650\uff0c\u5e76\u5728\u590d\u5236\u4e4b\u540e\u6062\u590d\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>import stat<\/p>\n<p>source = &#39;path\/to\/source\/folder&#39;<\/p>\n<p>destination = &#39;path\/to\/destination\/folder&#39;<\/p>\n<p>def get_permissions(path):<\/p>\n<p>    return stat.S_IMODE(os.lstat(path).st_mode)<\/p>\n<p>def set_permissions(path, permissions):<\/p>\n<p>    os.chmod(path, permissions)<\/p>\n<p>permissions = {}<\/p>\n<p>for root, dirs, files in os.walk(source):<\/p>\n<p>    for file in files:<\/p>\n<p>        path = os.path.join(root, file)<\/p>\n<p>        permissions[path] = get_permissions(path)<\/p>\n<p>        os.chmod(path, stat.S_IWUSR)<\/p>\n<p>shutil.copytree(source, destination)<\/p>\n<p>for root, dirs, files in os.walk(destination):<\/p>\n<p>    for file in files:<\/p>\n<p>        path = os.path.join(root, file)<\/p>\n<p>        original_path = os.path.join(source, os.path.relpath(path, destination))<\/p>\n<p>        if original_path in permissions:<\/p>\n<p>            set_permissions(path, permissions[original_path])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86get_permissions()\u548cset_permissions()\u51fd\u6570\uff0c\u7528\u6765\u83b7\u53d6\u548c\u8bbe\u7f6e\u6587\u4ef6\u6743\u9650\u3002\u5728\u590d\u5236\u4e4b\u524d\uff0c\u6211\u4eec\u8bb0\u5f55\u6e90\u6587\u4ef6\u5939\u4e2d\u6240\u6709\u6587\u4ef6\u7684\u6743\u9650\uff0c\u5e76\u5728\u590d\u5236\u4e4b\u540e\u6062\u590d\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\u590d\u5236\u6587\u4ef6\u5939\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6700\u5e38\u7528\u548c\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528shutil\u6a21\u5757\u7684shutil.copytree()\u51fd\u6570\u3002<\/strong> \u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u4f60\u5b66\u4f1a\u4e86\u5982\u4f55\u4f7f\u7528shutil\u6a21\u5757\u3001os\u6a21\u5757\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u6765\u590d\u5236\u6587\u4ef6\u5939\uff0c\u5e76\u5904\u7406\u6587\u4ef6\u6743\u9650\u95ee\u9898\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6587\u4ef6\u5939\u590d\u5236\uff0c\u8fd8\u662f\u590d\u6742\u7684\u6587\u4ef6\u7c7b\u578b\u7b5b\u9009\u548c\u6743\u9650\u5904\u7406\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\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\u5939\u53ca\u5176\u5185\u5bb9\uff1f<\/strong><br \/>\u8981\u590d\u5236\u4e00\u4e2a\u6587\u4ef6\u5939\u53ca\u5176\u6240\u6709\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shutil<\/code>\u6a21\u5757\u4e2d\u7684<code>copytree<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u80fd\u591f\u9012\u5f52\u5730\u590d\u5236\u6587\u4ef6\u5939\u53ca\u5176\u5305\u542b\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import shutil\n\nshutil.copytree(&#39;source_folder&#39;, &#39;destination_folder&#39;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u6267\u884c\u6b64\u64cd\u4f5c\u4e4b\u524d\uff0c\u76ee\u6807\u6587\u4ef6\u5939\u4e0d\u5b58\u5728\u3002\u5982\u679c\u76ee\u6807\u6587\u4ef6\u5939\u5df2\u5b58\u5728\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ignore<\/code>\u53c2\u6570\u6765\u51b3\u5b9a\u5982\u4f55\u5904\u7406\u3002<\/p>\n<p><strong>\u590d\u5236\u6587\u4ef6\u5939\u65f6\u5982\u4f55\u5904\u7406\u6587\u4ef6\u5939\u4e2d\u7684\u5b50\u6587\u4ef6\u5939\u548c\u6587\u4ef6\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>shutil.copytree<\/code>\u51fd\u6570\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u5904\u7406\u5b50\u6587\u4ef6\u5939\u548c\u6587\u4ef6\u7684\u590d\u5236\u3002\u5982\u679c\u9700\u8981\u8fdb\u884c\u7279\u5b9a\u7684\u6587\u4ef6\u8fc7\u6ee4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ignore<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u54ea\u4e9b\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u5e94\u8be5\u88ab\u5ffd\u7565\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import shutil\n\ndef ignore_patterns(path, names):\n    return [name for name in names if name.endswith(&#39;.tmp&#39;)]\n\nshutil.copytree(&#39;source_folder&#39;, &#39;destination_folder&#39;, ignore=ignore_patterns)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u590d\u5236\u6587\u4ef6\u5939\uff0c\u540c\u65f6\u5ffd\u7565\u6240\u6709\u4ee5<code>.tmp<\/code>\u7ed3\u5c3e\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728\u590d\u5236\u6587\u4ef6\u5939\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6587\u4ef6\u6743\u9650\u548c\u5143\u6570\u636e\uff1f<\/strong><br \/>\u4f7f\u7528<code>shutil.copytree<\/code>\u65f6\uff0c\u9ed8\u8ba4\u4f1a\u4fdd\u7559\u6587\u4ef6\u7684\u6743\u9650\u548c\u5143\u6570\u636e\u3002\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49\u8fd9\u4e9b\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>copy_function<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u81ea\u5b9a\u4e49\u7684\u590d\u5236\u51fd\u6570\u3002\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u53ef\u4ee5\u63a7\u5236\u5982\u4f55\u5904\u7406\u6587\u4ef6\u7684\u6743\u9650\u548c\u5143\u6570\u636e\uff0c\u786e\u4fdd\u5728\u590d\u5236\u8fc7\u7a0b\u4e2d\u7b26\u5408\u7279\u5b9a\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u590d\u5236\u4e00\u4e2a\u6587\u4ef6\u5939\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528shutil\u6a21\u5757\u3001os\u6a21\u5757\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u5982 distuti [&hellip;]","protected":false},"author":3,"featured_media":1118220,"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\/1118191"}],"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=1118191"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118191\/revisions"}],"predecessor-version":[{"id":1118225,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118191\/revisions\/1118225"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1118220"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1118191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1118191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1118191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}