{"id":966612,"date":"2024-12-27T04:49:11","date_gmt":"2024-12-26T20:49:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/966612.html"},"modified":"2024-12-27T04:49:12","modified_gmt":"2024-12-26T20:49:12","slug":"python%e5%a6%82%e4%bd%95%e6%8b%b7%e8%b4%9d%e7%9b%ae%e5%bd%95%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/966612.html","title":{"rendered":"python\u5982\u4f55\u62f7\u8d1d\u76ee\u5f55\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182312\/758d9d5e-8805-4bce-9164-577d6daec3b6.webp\" alt=\"python\u5982\u4f55\u62f7\u8d1d\u76ee\u5f55\u6587\u4ef6\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u62f7\u8d1d\u76ee\u5f55\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shutil<\/code>\u6a21\u5757\uff0c<strong>\u4e3b\u8981\u7684\u65b9\u6cd5\u5305\u62ec\uff1a<code>shutil.copytree()<\/code>\u7528\u4e8e\u62f7\u8d1d\u6574\u4e2a\u76ee\u5f55\u3001<code>os.walk()<\/code>\u7ed3\u5408<code>shutil.copy()<\/code>\u7528\u4e8e\u9010\u4e2a\u6587\u4ef6\u62f7\u8d1d<\/strong>\u3002<code>shutil.copytree()<\/code>\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u4f60\u5c06\u6574\u4e2a\u76ee\u5f55\u53ca\u5176\u5185\u5bb9\u590d\u5236\u5230\u65b0\u7684\u76ee\u6807\u4f4d\u7f6e\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>shutil.copytree()<\/code><\/p>\n<\/p>\n<p><p><code>shutil.copytree()<\/code>\u662fPython\u6807\u51c6\u5e93\u4e2d\u4e13\u95e8\u7528\u4e8e\u590d\u5236\u76ee\u5f55\u7684\u51fd\u6570\u3002\u5b83\u4f1a\u9012\u5f52\u5730\u5c06\u6e90\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u590d\u5236\u5230\u76ee\u6807\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>def copy_directory(source, destination):<\/p>\n<p>    try:<\/p>\n<p>        shutil.copytree(source, destination)<\/p>\n<p>        print(f&quot;Directory &#39;{source}&#39; copied successfully to &#39;{destination}&#39;&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u7840\u7528\u6cd5<\/strong><br \/>\u5728\u4f7f\u7528<code>shutil.copytree()<\/code>\u65f6\uff0c\u9700\u8981\u63d0\u4f9b\u6e90\u76ee\u5f55\u548c\u76ee\u6807\u76ee\u5f55\u7684\u8def\u5f84\u3002\u8be5\u51fd\u6570\u4f1a\u521b\u5efa\u76ee\u6807\u76ee\u5f55\uff08\u5982\u679c\u4e0d\u5b58\u5728\uff09\uff0c\u7136\u540e\u5c06\u6e90\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u9012\u5f52\u590d\u5236\u5230\u76ee\u6807\u76ee\u5f55\u4e2d\u3002\u8fd9\u975e\u5e38\u9002\u5408\u4e8e\u9700\u8981\u590d\u5236\u6574\u4e2a\u76ee\u5f55\u7ed3\u6784\u548c\u5185\u5bb9\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9519\u8bef\u5904\u7406<\/strong><br \/>\u4f7f\u7528<code>shutil.copytree()<\/code>\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\uff0c\u6bd4\u5982\u76ee\u6807\u76ee\u5f55\u5df2\u7ecf\u5b58\u5728\u3001\u6e90\u76ee\u5f55\u4e0d\u5b58\u5728\u7b49\u3002\u4e3a\u4e86\u589e\u5f3a\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u5e94\u8be5\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u5982\u679c\u76ee\u6807\u76ee\u5f55\u5df2\u7ecf\u5b58\u5728\uff0c\u53ef\u4ee5\u5728\u6355\u83b7\u5f02\u5e38\u540e\u5220\u9664\u8be5\u76ee\u5f55\u6216\u9009\u62e9\u4e0d\u540c\u7684\u76ee\u6807\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u590d\u5236\u9009\u9879<\/strong><br \/><code>shutil.copytree()<\/code>\u63d0\u4f9b\u4e86\u4e00\u4e9b\u53ef\u9009\u7684\u53c2\u6570\uff0c\u6bd4\u5982<code>ignore<\/code>\u53c2\u6570\u53ef\u4ee5\u7528\u4e8e\u5ffd\u7565\u67d0\u4e9b\u6587\u4ef6\u6216\u76ee\u5f55\u3002\u5728\u9700\u8981\u5ffd\u7565\u67d0\u4e9b\u4e0d\u9700\u8981\u590d\u5236\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u65f6\uff0c\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a\u5ffd\u7565\u51fd\u6570\uff0c\u6bd4\u5982<code>shutil.ignore_patterns()<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>os.walk()<\/code>\u7ed3\u5408<code>shutil.copy()<\/code><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5bf9\u590d\u5236\u8fc7\u7a0b\u8fdb\u884c\u66f4\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\uff0c\u6bd4\u5982\u53ea\u590d\u5236\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\u3002\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528<code>os.walk()<\/code>\u7ed3\u5408<code>shutil.copy()<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>def copy_files(source, destination):<\/p>\n<p>    for root, dirs, files in os.walk(source):<\/p>\n<p>        # \u6784\u5efa\u76ee\u6807\u8def\u5f84<\/p>\n<p>        dest_dir = root.replace(source, destination, 1)<\/p>\n<p>        if not os.path.exists(dest_dir):<\/p>\n<p>            os.makedirs(dest_dir)<\/p>\n<p>        for file in files:<\/p>\n<p>            # \u590d\u5236\u6587\u4ef6<\/p>\n<p>            shutil.copy(os.path.join(root, file), dest_dir)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li>\n<p><strong>\u904d\u5386\u76ee\u5f55<\/strong><br \/><code>os.walk()<\/code>\u7528\u4e8e\u9012\u5f52\u904d\u5386\u76ee\u5f55\u6811\uff0c\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u5668\uff0c\u751f\u6210\u4e00\u4e2a\u4e09\u5143\u7ec4<code>(dirpath, dirnames, filenames)<\/code>\uff0c\u5206\u522b\u8868\u793a\u5f53\u524d\u904d\u5386\u7684\u76ee\u5f55\u8def\u5f84\u3001\u76ee\u5f55\u4e2d\u7684\u5b50\u76ee\u5f55\u5217\u8868\u548c\u6587\u4ef6\u5217\u8868\u3002\u901a\u8fc7\u904d\u5386\u8fd9\u4e9b\u8def\u5f84\u548c\u6587\u4ef6\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5b9e\u73b0\u5bf9\u76ee\u5f55\u4e2d\u5185\u5bb9\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u76ee\u6807\u76ee\u5f55<\/strong><br \/>\u4f7f\u7528<code>os.makedirs()<\/code>\u521b\u5efa\u76ee\u6807\u76ee\u5f55\u3002\u4e3a\u4e86\u4fdd\u6301\u6e90\u76ee\u5f55\u7684\u7ed3\u6784\uff0c\u9700\u8981\u5728\u6bcf\u6b21\u904d\u5386\u65f6\u6784\u5efa\u5bf9\u5e94\u7684\u76ee\u6807\u76ee\u5f55\u8def\u5f84\uff0c\u5e76\u5728\u8be5\u8def\u5f84\u4e0d\u5b58\u5728\u65f6\u521b\u5efa\u5b83\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u590d\u5236\u6587\u4ef6<\/strong><br \/>\u4f7f\u7528<code>shutil.copy()<\/code>\u590d\u5236\u6587\u4ef6\uff0c\u53c2\u6570\u4e3a\u6e90\u6587\u4ef6\u8def\u5f84\u548c\u76ee\u6807\u6587\u4ef6\u8def\u5f84\u3002\u901a\u8fc7<code>os.path.join()<\/code>\u6784\u5efa\u5b8c\u6574\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u786e\u4fdd\u6587\u4ef6\u88ab\u590d\u5236\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>distutils.dir_util.copy_tree()<\/code><\/p>\n<\/p>\n<p><p><code>distutils.dir_util.copy_tree()<\/code>\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u590d\u5236\u76ee\u5f55\u7684\u65b9\u6cd5\u3002\u867d\u7136<code>distutils<\/code>\u6a21\u5757\u5728Python 3.10\u4e2d\u5df2\u88ab\u5f03\u7528\uff0c\u4f46\u5728\u65e7\u7248\u672c\u4e2d\u4ecd\u7136\u53ef\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from distutils.dir_util import copy_tree<\/p>\n<p>def copy_directory_distutils(source, destination):<\/p>\n<p>    try:<\/p>\n<p>        copy_tree(source, destination)<\/p>\n<p>        print(f&quot;Directory &#39;{source}&#39; copied successfully to &#39;{destination}&#39; using distutils&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li>\n<p><strong>\u66ff\u4ee3\u65b9\u6848<\/strong><br \/><code>distutils.dir_util.copy_tree()<\/code>\u4e5f\u662f\u9012\u5f52\u590d\u5236\u76ee\u5f55\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u4f46\u7531\u4e8e<code>distutils<\/code>\u6a21\u5757\u5728\u8f83\u65b0\u7248\u672c\u4e2d\u5df2\u88ab\u5f03\u7528\uff0c\u56e0\u6b64\u5728\u65b0\u9879\u76ee\u4e2d\u4e0d\u5efa\u8bae\u4f7f\u7528\u3002\u5982\u679c\u5728\u65e7\u9879\u76ee\u4e2d\u4f7f\u7528\uff0c\u53ef\u4ee5\u8003\u8651\u9010\u6b65\u8fc1\u79fb\u5230<code>shutil<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7b80\u5355\u6613\u7528<\/strong><br \/><code>copy_tree()<\/code>\u7684\u7528\u6cd5\u4e0e<code>shutil.copytree()<\/code>\u7c7b\u4f3c\uff0c\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u63d0\u4f9b\u6e90\u76ee\u5f55\u548c\u76ee\u6807\u76ee\u5f55\u7684\u8def\u5f84\u5373\u53ef\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u517c\u5bb9\u6027\u6ce8\u610f<\/strong><br \/>\u7531\u4e8e<code>distutils<\/code>\u6a21\u5757\u9010\u6e10\u88ab\u5f03\u7528\uff0c\u5efa\u8bae\u5728\u65b0\u9879\u76ee\u4e2d\u5c3d\u91cf\u4f7f\u7528<code>shutil<\/code>\u6a21\u5757\u8fdb\u884c\u76ee\u5f55\u590d\u5236\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<code>pathlib<\/code><\/p>\n<\/p>\n<p><p><code>pathlib<\/code>\u662fPython 3.4\u5f15\u5165\u7684\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u7684\u6a21\u5757\u3002\u867d\u7136<code>pathlib<\/code>\u672c\u8eab\u4e0d\u76f4\u63a5\u652f\u6301\u590d\u5236\u76ee\u5f55\uff0c\u4f46\u53ef\u4ee5\u4e0e<code>shutil<\/code>\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>import shutil<\/p>\n<p>def copy_directory_pathlib(source, destination):<\/p>\n<p>    source_path = Path(source)<\/p>\n<p>    destination_path = Path(destination)<\/p>\n<p>    try:<\/p>\n<p>        shutil.copytree(source_path, destination_path)<\/p>\n<p>        print(f&quot;Directory &#39;{source}&#39; copied successfully to &#39;{destination}&#39; using pathlib&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8def\u5f84\u5bf9\u8c61<\/strong><br \/><code>pathlib.Path<\/code>\u5bf9\u8c61\u8868\u793a\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\uff0c\u652f\u6301\u591a\u79cd\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5e76\u4e14\u5728\u8de8\u5e73\u53f0\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u63d0\u4f9b\u66f4\u597d\u7684\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e0e<code>shutil<\/code>\u7ed3\u5408\u4f7f\u7528<\/strong><br \/><code>pathlib<\/code>\u53ef\u4ee5\u4e0e<code>shutil<\/code>\u7ed3\u5408\u4f7f\u7528\uff0c\u901a\u8fc7\u5c06<code>Path<\/code>\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>shutil<\/code>\u51fd\u6570\uff0c\u5b9e\u73b0\u8def\u5f84\u64cd\u4f5c\u548c\u76ee\u5f55\u590d\u5236\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u73b0\u4ee3\u5316\u7684\u8def\u5f84\u5904\u7406<\/strong><br \/><code>pathlib<\/code>\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u5316\u7684\u8def\u5f84\u5904\u7406\u65b9\u5f0f\uff0c\u76f8\u8f83\u4e8e<code>os<\/code>\u6a21\u5757\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u4f7f\u7528\u3002\u5728\u65b0\u9879\u76ee\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528<code>pathlib<\/code>\u5904\u7406\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u62f7\u8d1d\u76ee\u5f55\u6587\u4ef6\u7684\u5e38\u7528\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec<code>shutil.copytree()<\/code>\u3001\u7ed3\u5408<code>os.walk()<\/code>\u548c<code>shutil.copy()<\/code>\u3001<code>distutils.dir_util.copy_tree()<\/code>\uff0c\u4ee5\u53ca\u4e0e<code>pathlib<\/code>\u7ed3\u5408\u4f7f\u7528\u7684<code>shutil.copytree()<\/code>\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong><br \/>\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5982\u679c\u9700\u8981\u7b80\u5355\u5730\u590d\u5236\u6574\u4e2a\u76ee\u5f55\uff0c<code>shutil.copytree()<\/code>\u662f\u6700\u4f73\u9009\u62e9\u3002\u5982\u679c\u9700\u8981\u5bf9\u590d\u5236\u8fc7\u7a0b\u8fdb\u884c\u66f4\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\uff0c\u7ed3\u5408<code>os.walk()<\/code>\u548c<code>shutil.copy()<\/code>\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u5f02\u5e38<\/strong><br \/>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5e94\u6ce8\u610f\u6355\u83b7\u5e76\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\uff0c\u4ee5\u589e\u5f3a\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002\u5728\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u65f6\uff0c\u5e38\u89c1\u7684\u5f02\u5e38\u5305\u62ec\u6743\u9650\u9519\u8bef\u3001\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u76ee\u6807\u76ee\u5f55\u5df2\u5b58\u5728\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8def\u5f84\u5904\u7406<\/strong><br \/>\u5728\u5904\u7406\u8def\u5f84\u65f6\uff0c\u63a8\u8350\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u5316\u548c\u76f4\u89c2\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u65f6\uff0c<code>pathlib<\/code>\u80fd\u591f\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u5dee\u5f02\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u5b9e\u8df5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u5730\u5b9e\u73b0\u76ee\u5f55\u6587\u4ef6\u7684\u590d\u5236\u3002\u5e0c\u671b\u8fd9\u4e9b\u6280\u5de7\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6587\u4ef6\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u62f7\u8d1d\u6574\u4e2a\u76ee\u5f55\u53ca\u5176\u5185\u5bb9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>shutil<\/code>\u6a21\u5757\u4e2d\u7684<code>copytree<\/code>\u51fd\u6570\u6765\u62f7\u8d1d\u6574\u4e2a\u76ee\u5f55\u53ca\u5176\u5305\u542b\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u3002\u8fd9\u4e2a\u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a\u6e90\u76ee\u5f55\u548c\u76ee\u6807\u76ee\u5f55\u3002\u4f8b\u5982\uff0c<code>shutil.copytree(&#39;source_directory&#39;, &#39;destination_directory&#39;)<\/code>\u5c06\u4f1a\u628a<code>source_directory<\/code>\u4e2d\u7684\u6240\u6709\u5185\u5bb9\u590d\u5236\u5230<code>destination_directory<\/code>\u3002<\/p>\n<p><strong>\u5728\u62f7\u8d1d\u76ee\u5f55\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6587\u4ef6\u540d\u51b2\u7a81\uff1f<\/strong><br \/>\u5f53\u76ee\u6807\u76ee\u5f55\u5df2\u5b58\u5728\uff0c<code>shutil.copytree<\/code>\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u5148\u68c0\u67e5\u76ee\u6807\u76ee\u5f55\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u5b58\u5728\uff0c\u53ef\u4ee5\u9009\u62e9\u5220\u9664\u5b83\u6216\u8005\u4f7f\u7528\u5176\u4ed6\u65b9\u5f0f\u5904\u7406\u51b2\u7a81\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>os.path.exists()<\/code>\u68c0\u67e5\u76ee\u6807\u76ee\u5f55\uff0c\u82e5\u5b58\u5728\u5219\u4f7f\u7528<code>shutil.rmtree()<\/code>\u5220\u9664\u5b83\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Python\u62f7\u8d1d\u76ee\u5f55\u4e2d\u7684\u7279\u5b9a\u6587\u4ef6\u7c7b\u578b\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<code>shutil.copytree<\/code>\u5141\u8bb8\u4f20\u5165\u4e00\u4e2a<code>ignore<\/code>\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u53ef\u4ee5\u7528\u6765\u6307\u5b9a\u4e0d\u9700\u8981\u62f7\u8d1d\u7684\u6587\u4ef6\u7c7b\u578b\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u5ffd\u7565\u51fd\u6570\uff0c\u8fd4\u56de\u9700\u8981\u5ffd\u7565\u7684\u6587\u4ef6\u7c7b\u578b\u5217\u8868\uff0c\u53ef\u4ee5\u7075\u6d3b\u9009\u62e9\u8981\u62f7\u8d1d\u7684\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ignore=lambda src, names: [name for name in names if name.endswith(&#39;.txt&#39;)]<\/code>\u6765\u5ffd\u7565\u6240\u6709<code>.txt<\/code>\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u62f7\u8d1d\u76ee\u5f55\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528shutil\u6a21\u5757\uff0c\u4e3b\u8981\u7684\u65b9\u6cd5\u5305\u62ec\uff1ashutil.copytree()\u7528\u4e8e [&hellip;]","protected":false},"author":3,"featured_media":966616,"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\/966612"}],"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=966612"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/966612\/revisions"}],"predecessor-version":[{"id":966617,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/966612\/revisions\/966617"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/966616"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=966612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=966612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=966612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}