{"id":1116911,"date":"2025-01-08T18:21:00","date_gmt":"2025-01-08T10:21:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1116911.html"},"modified":"2025-01-08T18:21:08","modified_gmt":"2025-01-08T10:21:08","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e9%87%8d%e5%bb%ba%e6%96%87%e4%bb%b6%e5%a4%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1116911.html","title":{"rendered":"\u5982\u4f55\u5728Python\u4e2d\u91cd\u5efa\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\/25081038\/753978d6-be8a-4640-87c6-7a7202fc5d97.webp\" alt=\"\u5982\u4f55\u5728Python\u4e2d\u91cd\u5efa\u6587\u4ef6\u5939\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u91cd\u5efa\u6587\u4ef6\u5939\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3001\u7ba1\u7406\u6587\u4ef6\u6743\u9650<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u91cd\u5efa\u6587\u4ef6\u5939\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5176\u4e2d\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u548cpathlib\u6a21\u5757\u3002\u8fd9\u4e24\u4e2a\u6a21\u5757\u90fd\u63d0\u4f9b\u4e86\u521b\u5efa\u548c\u7ba1\u7406\u6587\u4ef6\u5939\u7684\u529f\u80fd\u3002\u5177\u4f53\u64cd\u4f5c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/h3>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528os\u6a21\u5757\u91cd\u5efa\u6587\u4ef6\u5939\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165os\u6a21\u5757<\/strong><\/li>\n<li><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/strong><\/li>\n<li><strong>\u5220\u9664\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939<\/strong><\/li>\n<li><strong>\u521b\u5efa\u65b0\u7684\u6587\u4ef6\u5939<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>folder_path = &#39;path\/to\/folder&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(folder_path):<\/p>\n<p>    # \u5220\u9664\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939<\/p>\n<p>    shutil.rmtree(folder_path)<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u7684\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>os.makedirs(folder_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528pathlib\u6a21\u5757\u91cd\u5efa\u6587\u4ef6\u5939\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165pathlib\u6a21\u5757<\/strong><\/li>\n<li><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/strong><\/li>\n<li><strong>\u5220\u9664\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939<\/strong><\/li>\n<li><strong>\u521b\u5efa\u65b0\u7684\u6587\u4ef6\u5939<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>folder_path = Path(&#39;path\/to\/folder&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if folder_path.exists():<\/p>\n<p>    # \u5220\u9664\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939<\/p>\n<p>    for child in folder_path.iterdir():<\/p>\n<p>        if child.is_file():<\/p>\n<p>            child.unlink()<\/p>\n<p>        else:<\/p>\n<p>            shutil.rmtree(child)<\/p>\n<p>    folder_path.rmdir()<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u7684\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>folder_path.mkdir(parents=True, exist_ok=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f8b\u5982\uff0c\u6587\u4ef6\u5939\u53ef\u80fd\u88ab\u5176\u4ed6\u8fdb\u7a0b\u5360\u7528\uff0c\u5bfc\u81f4\u5220\u9664\u64cd\u4f5c\u5931\u8d25\u3002\u4ee5\u4e0b\u662f\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>folder_path = &#39;path\/to\/folder&#39;<\/p>\n<p>try:<\/p>\n<p>    # \u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/p>\n<p>    if os.path.exists(folder_path):<\/p>\n<p>        # \u5220\u9664\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939<\/p>\n<p>        shutil.rmtree(folder_path)<\/p>\n<p>    # \u521b\u5efa\u65b0\u7684\u6587\u4ef6\u5939<\/p>\n<p>    os.makedirs(folder_path)<\/p>\n<p>except OSError as e:<\/p>\n<p>    print(f&quot;Error: {e.strerror}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ba1\u7406\u6587\u4ef6\u6743\u9650<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u7ba1\u7406\u6587\u4ef6\u5939\u7684\u6743\u9650\uff0c\u4f8b\u5982\u8bbe\u7f6e\u6587\u4ef6\u5939\u7684\u8bfb\u5199\u6743\u9650\u3002\u4ee5\u4e0b\u662f\u8bbe\u7f6e\u6587\u4ef6\u5939\u6743\u9650\u7684\u793a\u4f8b\uff1a<\/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>folder_path = &#39;path\/to\/folder&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(folder_path):<\/p>\n<p>    # \u5220\u9664\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939<\/p>\n<p>    shutil.rmtree(folder_path)<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u7684\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>os.makedirs(folder_path)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6587\u4ef6\u5939\u6743\u9650\uff08\u4f8b\u5982\uff1a\u8bfb\u5199\u6743\u9650\uff09<\/strong><\/h2>\n<p>os.chmod(folder_path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u4f7f\u7528os\u6a21\u5757\u5220\u9664\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528os\u6a21\u5757\u5220\u9664\u6587\u4ef6\u5939\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>shutil.rmtree<\/code>\u51fd\u6570\u6765\u9012\u5f52\u5220\u9664\u6587\u4ef6\u5939\u53ca\u5176\u5185\u5bb9\u3002<code>shutil.rmtree<\/code>\u51fd\u6570\u4f1a\u5c1d\u8bd5\u5220\u9664\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u6587\u4ef6\u5939\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165os\u548cshutil\u6a21\u5757<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5bfc\u5165\u8fd9\u4e24\u4e2a\u6a21\u5757\u3002<\/li>\n<li><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/strong>\uff1a\u4f7f\u7528<code>os.path.exists<\/code>\u51fd\u6570\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002<\/li>\n<li><strong>\u5220\u9664\u6587\u4ef6\u5939<\/strong>\uff1a\u5982\u679c\u6587\u4ef6\u5939\u5b58\u5728\uff0c\u4f7f\u7528<code>shutil.rmtree<\/code>\u51fd\u6570\u5220\u9664\u6587\u4ef6\u5939\u53ca\u5176\u5185\u5bb9\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>folder_path = &#39;path\/to\/folder&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(folder_path):<\/p>\n<p>    # \u5220\u9664\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939<\/p>\n<p>    shutil.rmtree(folder_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>shutil.rmtree<\/code>\u51fd\u6570\u5728\u5220\u9664\u6587\u4ef6\u5939\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u95ee\u9898\u6216\u6587\u4ef6\u5939\u88ab\u5176\u4ed6\u8fdb\u7a0b\u5360\u7528\u7684\u60c5\u51b5\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/p>\n<p>    if os.path.exists(folder_path):<\/p>\n<p>        # \u5220\u9664\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939<\/p>\n<p>        shutil.rmtree(folder_path)<\/p>\n<p>except PermissionError as e:<\/p>\n<p>    print(f&quot;PermissionError: {e}&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<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u4f7f\u7528pathlib\u6a21\u5757\u5220\u9664\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7b80\u6d01\u548c\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528pathlib\u6a21\u5757\u5220\u9664\u6587\u4ef6\u5939\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165pathlib\u6a21\u5757<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5bfc\u5165pathlib\u6a21\u5757\u3002<\/li>\n<li><strong>\u521b\u5efaPath\u5bf9\u8c61<\/strong>\uff1a\u4f7f\u7528<code>Path<\/code>\u7c7b\u521b\u5efa\u4e00\u4e2a\u8868\u793a\u6587\u4ef6\u5939\u8def\u5f84\u7684\u5bf9\u8c61\u3002<\/li>\n<li><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/strong>\uff1a\u4f7f\u7528<code>Path.exists<\/code>\u65b9\u6cd5\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002<\/li>\n<li><strong>\u5220\u9664\u6587\u4ef6\u5939\u53ca\u5176\u5185\u5bb9<\/strong>\uff1a\u5982\u679c\u6587\u4ef6\u5939\u5b58\u5728\uff0c\u9012\u5f52\u5220\u9664\u6587\u4ef6\u5939\u53ca\u5176\u5185\u5bb9\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>import shutil<\/p>\n<p>folder_path = Path(&#39;path\/to\/folder&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if folder_path.exists():<\/p>\n<p>    # \u5220\u9664\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939\u53ca\u5176\u5185\u5bb9<\/p>\n<p>    for child in folder_path.iterdir():<\/p>\n<p>        if child.is_file():<\/p>\n<p>            child.unlink()<\/p>\n<p>        else:<\/p>\n<p>            shutil.rmtree(child)<\/p>\n<p>    folder_path.rmdir()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u4f7f\u7528pathlib\u6a21\u5757\u5220\u9664\u6587\u4ef6\u5939\u53ca\u5176\u5185\u5bb9\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5220\u9664\u64cd\u4f5c\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u95ee\u9898\u6216\u6587\u4ef6\u5939\u88ab\u5176\u4ed6\u8fdb\u7a0b\u5360\u7528\u7684\u60c5\u51b5\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/p>\n<p>    if folder_path.exists():<\/p>\n<p>        # \u5220\u9664\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939\u53ca\u5176\u5185\u5bb9<\/p>\n<p>        for child in folder_path.iterdir():<\/p>\n<p>            if child.is_file():<\/p>\n<p>                child.unlink()<\/p>\n<p>            else:<\/p>\n<p>                shutil.rmtree(child)<\/p>\n<p>        folder_path.rmdir()<\/p>\n<p>except PermissionError as e:<\/p>\n<p>    print(f&quot;PermissionError: {e}&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<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f8b\u5982\uff0c\u6587\u4ef6\u5939\u53ef\u80fd\u88ab\u5176\u4ed6\u8fdb\u7a0b\u5360\u7528\uff0c\u5bfc\u81f4\u5220\u9664\u64cd\u4f5c\u5931\u8d25\u3002\u4ee5\u4e0b\u662f\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165os\u548cshutil\u6a21\u5757<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5bfc\u5165\u8fd9\u4e24\u4e2a\u6a21\u5757\u3002<\/li>\n<li><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/strong>\uff1a\u4f7f\u7528<code>os.path.exists<\/code>\u51fd\u6570\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002<\/li>\n<li><strong>\u5220\u9664\u6587\u4ef6\u5939\u53ca\u5176\u5185\u5bb9<\/strong>\uff1a\u5982\u679c\u6587\u4ef6\u5939\u5b58\u5728\uff0c\u4f7f\u7528<code>shutil.rmtree<\/code>\u51fd\u6570\u5220\u9664\u6587\u4ef6\u5939\u53ca\u5176\u5185\u5bb9\u3002<\/li>\n<li><strong>\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38<\/strong>\uff1a\u4f7f\u7528try-except\u5757\u6355\u83b7\u548c\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>folder_path = &#39;path\/to\/folder&#39;<\/p>\n<p>try:<\/p>\n<p>    # \u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/p>\n<p>    if os.path.exists(folder_path):<\/p>\n<p>        # \u5220\u9664\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939<\/p>\n<p>        shutil.rmtree(folder_path)<\/p>\n<p>    # \u521b\u5efa\u65b0\u7684\u6587\u4ef6\u5939<\/p>\n<p>    os.makedirs(folder_path)<\/p>\n<p>except PermissionError as e:<\/p>\n<p>    print(f&quot;PermissionError: {e}&quot;)<\/p>\n<p>except OSError as e:<\/p>\n<p>    print(f&quot;Error: {e.strerror}&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;Unexpected error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u5220\u9664\u6587\u4ef6\u5939\u65f6\u53ef\u80fd\u9047\u5230\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u53ef\u80fd\u9700\u8981\u4e0d\u540c\u7684\u5904\u7406\u65b9\u5f0f\uff0c\u56e0\u6b64\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u7ba1\u7406\u6587\u4ef6\u6743\u9650<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u7ba1\u7406\u6587\u4ef6\u5939\u7684\u6743\u9650\uff0c\u4f8b\u5982\u8bbe\u7f6e\u6587\u4ef6\u5939\u7684\u8bfb\u5199\u6743\u9650\u3002\u4ee5\u4e0b\u662f\u8bbe\u7f6e\u6587\u4ef6\u5939\u6743\u9650\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165os\u548cstat\u6a21\u5757<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5bfc\u5165\u8fd9\u4e24\u4e2a\u6a21\u5757\u3002<\/li>\n<li><strong>\u521b\u5efa\u65b0\u7684\u6587\u4ef6\u5939<\/strong>\uff1a\u4f7f\u7528<code>os.makedirs<\/code>\u51fd\u6570\u521b\u5efa\u65b0\u7684\u6587\u4ef6\u5939\u3002<\/li>\n<li><strong>\u8bbe\u7f6e\u6587\u4ef6\u5939\u6743\u9650<\/strong>\uff1a\u4f7f\u7528<code>os.chmod<\/code>\u51fd\u6570\u8bbe\u7f6e\u6587\u4ef6\u5939\u7684\u8bfb\u5199\u6743\u9650\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/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>folder_path = &#39;path\/to\/folder&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(folder_path):<\/p>\n<p>    # \u5220\u9664\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939<\/p>\n<p>    shutil.rmtree(folder_path)<\/p>\n<h2><strong>\u521b\u5efa\u65b0\u7684\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>os.makedirs(folder_path)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6587\u4ef6\u5939\u6743\u9650\uff08\u4f8b\u5982\uff1a\u8bfb\u5199\u6743\u9650\uff09<\/strong><\/h2>\n<p>os.chmod(folder_path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u6587\u4ef6\u5939\u7684\u8bfb\u5199\u6743\u9650\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u6587\u4ef6\u6743\u9650\u7684\u5904\u7406\u65b9\u5f0f\u53ef\u80fd\u6709\u6240\u4e0d\u540c\uff0c\u56e0\u6b64\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u91cd\u5efa\u6587\u4ef6\u5939\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5176\u4e2d\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u548cpathlib\u6a21\u5757\u3002\u8fd9\u4e24\u4e2a\u6a21\u5757\u90fd\u63d0\u4f9b\u4e86\u521b\u5efa\u548c\u7ba1\u7406\u6587\u4ef6\u5939\u7684\u529f\u80fd\u3002\u5177\u4f53\u64cd\u4f5c\u6b65\u9aa4\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165\u76f8\u5173\u6a21\u5757<\/li>\n<li>\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728<\/li>\n<li>\u5220\u9664\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5939\u53ca\u5176\u5185\u5bb9<\/li>\n<li>\u521b\u5efa\u65b0\u7684\u6587\u4ef6\u5939<\/li>\n<li>\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/li>\n<li>\u7ba1\u7406\u6587\u4ef6\u6743\u9650<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u91cd\u5efa\u6587\u4ef6\u5939\uff0c\u5e76\u6709\u6548\u5730\u5904\u7406\u53ef\u80fd\u9047\u5230\u7684\u5f02\u5e38\u60c5\u51b5\u548c\u6587\u4ef6\u6743\u9650\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u91cd\u5efa\u6587\u4ef6\u5939\u4e4b\u524d\uff0c\u4e86\u89e3\u5982\u4f55\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5df2\u5b58\u5728\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528<code>os.path.exists()<\/code>\u51fd\u6570\u6765\u5224\u65ad\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002\u5982\u679c\u8fd4\u56de\u503c\u4e3a<code>True<\/code>\uff0c\u5219\u8868\u793a\u6587\u4ef6\u5939\u5df2\u5b58\u5728\uff0c\u5982\u679c\u8fd4\u56de\u503c\u4e3a<code>False<\/code>\uff0c\u5219\u8868\u793a\u6587\u4ef6\u5939\u4e0d\u5b58\u5728\u3002<\/p>\n<p><strong>\u91cd\u5efa\u6587\u4ef6\u5939\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5df2\u5b58\u5728\u6587\u4ef6\u5939\u4e2d\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5f53\u91cd\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\u65f6\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8003\u8651\u5982\u4f55\u5904\u7406\u73b0\u6709\u6587\u4ef6\u5939\u4e2d\u7684\u6570\u636e\u3002\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u5305\u62ec\uff1a\u5220\u9664\u73b0\u6709\u6587\u4ef6\u5939\u53ca\u5176\u6240\u6709\u5185\u5bb9\uff0c\u7136\u540e\u518d\u521b\u5efa\u65b0\u6587\u4ef6\u5939\uff1b\u6216\u8005\uff0c\u5c06\u73b0\u6709\u6587\u4ef6\u5939\u7684\u5185\u5bb9\u79fb\u52a8\u5230\u53e6\u4e00\u4e2a\u4f4d\u7f6e\u3002\u53ef\u4ee5\u4f7f\u7528<code>shutil<\/code>\u6a21\u5757\u4e2d\u7684<code>shutil.rmtree()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u6587\u4ef6\u5939\u53ca\u5176\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5728\u91cd\u5efa\u6587\u4ef6\u5939\u65f6\uff0c\u5982\u4f55\u8bbe\u7f6e\u7279\u5b9a\u7684\u6743\u9650\uff1f<\/strong><br \/>Python\u5141\u8bb8\u60a8\u5728\u521b\u5efa\u6587\u4ef6\u5939\u65f6\u8bbe\u7f6e\u7279\u5b9a\u7684\u6743\u9650\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>os.makedirs()<\/code>\u51fd\u6570\u7684<code>mode<\/code>\u53c2\u6570\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u6307\u5b9a\u4e00\u4e2a\u516b\u8fdb\u5236\u6570\uff0c\u4f8b\u5982<code>0o755<\/code>\uff0c\u60a8\u53ef\u4ee5\u63a7\u5236\u6587\u4ef6\u5939\u7684\u8bfb\u3001\u5199\u548c\u6267\u884c\u6743\u9650\u3002\u521b\u5efa\u6587\u4ef6\u5939\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.chmod()<\/code>\u51fd\u6570\u6765\u66f4\u6539\u6743\u9650\u8bbe\u7f6e\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u91cd\u5efa\u7684\u6587\u4ef6\u5939\u662f\u7a7a\u7684\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u91cd\u5efa\u7684\u6587\u4ef6\u5939\u662f\u7a7a\u7684\uff0c\u53ef\u4ee5\u5728\u521b\u5efa\u4e4b\u524d\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u5e76\u6e05\u7a7a\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528<code>os.listdir()<\/code>\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5217\u8868\uff0c\u7136\u540e\u9010\u4e2a\u5220\u9664\u8fd9\u4e9b\u6587\u4ef6\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u65b0\u521b\u5efa\u7684\u6587\u4ef6\u5939\u4e0d\u4f1a\u5305\u542b\u4efb\u4f55\u65e7\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u91cd\u5efa\u6587\u4ef6\u5939\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u3001\u4f7f\u7528pathlib\u6a21\u5757\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3001\u7ba1\u7406\u6587\u4ef6\u6743\u9650 \u5728Py [&hellip;]","protected":false},"author":3,"featured_media":1116922,"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\/1116911"}],"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=1116911"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116911\/revisions"}],"predecessor-version":[{"id":1116926,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116911\/revisions\/1116926"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1116922"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1116911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1116911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1116911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}