{"id":1125169,"date":"2025-01-08T19:49:04","date_gmt":"2025-01-08T11:49:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1125169.html"},"modified":"2025-01-08T19:49:07","modified_gmt":"2025-01-08T11:49:07","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%96%b0%e5%bb%ba%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e5%a4%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1125169.html","title":{"rendered":"\u5982\u4f55\u7528python\u65b0\u5efa\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\/25085940\/a33ed0d5-68be-4d34-b68b-fc46d2b79236.webp\" alt=\"\u5982\u4f55\u7528python\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\" \/><\/p>\n<p><p> <strong>\u7528Python\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001\u6216\u8005shutil\u6a21\u5757\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\u3001\u6613\u4e8e\u7ef4\u62a4\u3001\u517c\u5bb9\u6027\u597d<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528os\u6a21\u5757\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757\u65b0\u5efa\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u975e\u5e38\u591a\u7684\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u63a5\u53e3\u3002\u4f7f\u7528os\u6a21\u5757\u65b0\u5efa\u6587\u4ef6\u5939\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u8c03\u7528<code>os.mkdir()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def create_folder(folder_name):<\/p>\n<p>    try:<\/p>\n<p>        os.mkdir(folder_name)<\/p>\n<p>        print(f&quot;Folder &#39;{folder_name}&#39; created successfully.&quot;)<\/p>\n<p>    except FileExistsError:<\/p>\n<p>        print(f&quot;Folder &#39;{folder_name}&#39; already exists.&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>create_folder(&quot;new_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p><code>os.mkdir()<\/code>\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u76ee\u5f55\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u8981\u521b\u5efa\u7684\u76ee\u5f55\u7684\u540d\u79f0\u6216\u8def\u5f84\u3002\u5982\u679c\u76ee\u5f55\u5df2\u7ecf\u5b58\u5728\uff0c\u4f1a\u629b\u51fa<code>FileExistsError<\/code>\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6700\u597d\u5148\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5b58\u5728\uff0c\u6216\u8005\u6355\u83b7\u8be5\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pathlib\u6a21\u5757\u65b0\u5efa\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\u3002\u5b83\u4f7f\u5f97\u8def\u5f84\u64cd\u4f5c\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def create_folder(folder_name):<\/p>\n<p>    path = Path(folder_name)<\/p>\n<p>    try:<\/p>\n<p>        path.mkdir()<\/p>\n<p>        print(f&quot;Folder &#39;{folder_name}&#39; created successfully.&quot;)<\/p>\n<p>    except FileExistsError:<\/p>\n<p>        print(f&quot;Folder &#39;{folder_name}&#39; already exists.&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>create_folder(&quot;new_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p><code>Path.mkdir()<\/code>\u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u76ee\u5f55\u3002\u4e0e<code>os.mkdir()<\/code>\u7c7b\u4f3c\uff0c\u5b83\u4e5f\u4f1a\u5728\u76ee\u5f55\u5df2\u7ecf\u5b58\u5728\u65f6\u629b\u51fa<code>FileExistsError<\/code>\u5f02\u5e38\u3002pathlib\u6a21\u5757\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u7684\u9762\u5411\u5bf9\u8c61\u7684\u8bbe\u8ba1\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528shutil\u6a21\u5757\u65b0\u5efa\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>shutil\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u9ad8\u7ea7\u7684\u6587\u4ef6\u64cd\u4f5c\uff0c\u5982\u590d\u5236\u3001\u79fb\u52a8\u3001\u5220\u9664\u6587\u4ef6\u7b49\u3002\u867d\u7136\u5b83\u4e0d\u662f\u4e13\u95e8\u7528\u4e8e\u521b\u5efa\u76ee\u5f55\u7684\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cshutil\u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>def create_folder(folder_name):<\/p>\n<p>    try:<\/p>\n<p>        shutil.os.mkdir(folder_name)<\/p>\n<p>        print(f&quot;Folder &#39;{folder_name}&#39; created successfully.&quot;)<\/p>\n<p>    except FileExistsError:<\/p>\n<p>        print(f&quot;Folder &#39;{folder_name}&#39; already exists.&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>create_folder(&quot;new_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u867d\u7136shutil\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u9ad8\u7ea7\u6587\u4ef6\u64cd\u4f5c\uff0c\u4f46\u5b83\u5b9e\u9645\u4e0a\u662f\u5c01\u88c5\u4e86os\u6a21\u5757\u7684\u4e00\u4e9b\u529f\u80fd\u3002\u6240\u4ee5\u4f7f\u7528shutil\u6a21\u5757\u521b\u5efa\u76ee\u5f55\u5b9e\u9645\u4e0a\u662f\u8c03\u7528\u4e86os\u6a21\u5757\u7684<code>os.mkdir()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u68c0\u67e5\u548c\u521b\u5efa\u591a\u7ea7\u76ee\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u521b\u5efa\u591a\u7ea7\u76ee\u5f55\uff08\u5373\u5d4c\u5957\u76ee\u5f55\uff09\u3002\u8fd9\u65f6\uff0c\u5355\u7eaf\u7684<code>os.mkdir()<\/code>\u6216<code>Path.mkdir()<\/code>\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ea\u80fd\u521b\u5efa\u4e00\u7ea7\u76ee\u5f55\u3002\u8981\u521b\u5efa\u591a\u7ea7\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.makedirs()<\/code>\u6216<code>Path.mkdir(parents=True)<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528os.makedirs()<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def create_folders(folder_path):<\/p>\n<p>    try:<\/p>\n<p>        os.makedirs(folder_path)<\/p>\n<p>        print(f&quot;Folders &#39;{folder_path}&#39; created successfully.&quot;)<\/p>\n<p>    except FileExistsError:<\/p>\n<p>        print(f&quot;Folders &#39;{folder_path}&#39; already exist.&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>create_folders(&quot;parent_folder\/child_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Path.mkdir(parents=True)<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def create_folders(folder_path):<\/p>\n<p>    path = Path(folder_path)<\/p>\n<p>    try:<\/p>\n<p>        path.mkdir(parents=True)<\/p>\n<p>        print(f&quot;Folders &#39;{folder_path}&#39; created successfully.&quot;)<\/p>\n<p>    except FileExistsError:<\/p>\n<p>        print(f&quot;Folders &#39;{folder_path}&#39; already exist.&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>create_folders(&quot;parent_folder\/child_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p><code>os.makedirs()<\/code>\u548c<code>Path.mkdir(parents=True)<\/code>\u90fd\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u591a\u7ea7\u76ee\u5f55\u3002\u5982\u679c\u76ee\u5f55\u5df2\u7ecf\u5b58\u5728\uff0c\u4f1a\u629b\u51fa<code>FileExistsError<\/code>\u5f02\u5e38\u3002\u4e0e<code>os.mkdir()<\/code>\u4e0d\u540c\u7684\u662f\uff0c\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u9012\u5f52\u5730\u521b\u5efa\u4e0d\u5b58\u5728\u7684\u7236\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84\u7684\u7279\u6b8a\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u8fd8\u9700\u8981\u8003\u8651\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u5982\u8def\u5f84\u5206\u9694\u7b26\u7684\u5dee\u5f02\u3001\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\u7684\u8f6c\u6362\u7b49\u3002Python\u63d0\u4f9b\u4e86<code>os.path<\/code>\u6a21\u5757\u548c<code>pathlib<\/code>\u6a21\u5757\u6765\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a\u5904\u7406\u8def\u5f84\u5206\u9694\u7b26<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def create_folder(folder_path):<\/p>\n<p>    normalized_path = os.path.normpath(folder_path)<\/p>\n<p>    try:<\/p>\n<p>        os.makedirs(normalized_path)<\/p>\n<p>        print(f&quot;Folder &#39;{normalized_path}&#39; created successfully.&quot;)<\/p>\n<p>    except FileExistsError:<\/p>\n<p>        print(f&quot;Folder &#39;{normalized_path}&#39; already exists.&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>create_folder(&quot;parent_folder\\\\child_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a\u76f8\u5bf9\u8def\u5f84\u548c\u7edd\u5bf9\u8def\u5f84\u7684\u8f6c\u6362<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def create_folder(folder_path):<\/p>\n<p>    path = Path(folder_path).resolve()<\/p>\n<p>    try:<\/p>\n<p>        path.mkdir(parents=True)<\/p>\n<p>        print(f&quot;Folder &#39;{path}&#39; created successfully.&quot;)<\/p>\n<p>    except FileExistsError:<\/p>\n<p>        print(f&quot;Folder &#39;{path}&#39; already exists.&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>create_folder(&quot;parent_folder\/child_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p><code>os.path.normpath()<\/code>\u53ef\u4ee5\u6807\u51c6\u5316\u8def\u5f84\u5206\u9694\u7b26\uff0c\u4f7f\u5f97\u4ee3\u7801\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5177\u6709\u66f4\u597d\u7684\u517c\u5bb9\u6027\u3002<code>Path.resolve()<\/code>\u53ef\u4ee5\u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u6362\u4e3a\u7edd\u5bf9\u8def\u5f84\uff0c\u907f\u514d\u8def\u5f84\u6df7\u6dc6\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5f02\u5e38\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u826f\u597d\u7684\u5f02\u5e38\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u548c\u8bb0\u5f55\u65e5\u5fd7\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u8c03\u8bd5\u548c\u7ef4\u62a4\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a\u5f02\u5e38\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import logging<\/p>\n<p>def create_folder(folder_path):<\/p>\n<p>    logging.basicConfig(filename=&#39;folder_creation.log&#39;, level=logging.INFO)<\/p>\n<p>    try:<\/p>\n<p>        os.makedirs(folder_path)<\/p>\n<p>        logging.info(f&quot;Folder &#39;{folder_path}&#39; created successfully.&quot;)<\/p>\n<p>    except FileExistsError:<\/p>\n<p>        logging.warning(f&quot;Folder &#39;{folder_path}&#39; already exists.&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        logging.error(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>create_folder(&quot;parent_folder\/child_folder&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528logging\u6a21\u5757\uff0c\u53ef\u4ee5\u5c06\u65e5\u5fd7\u4fe1\u606f\u8bb0\u5f55\u5230\u6587\u4ef6\u4e2d\u3002\u8fd9\u6837\uff0c\u5728\u4ee3\u7801\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u65e5\u5fd7\u6587\u4ef6\u6765\u4e86\u89e3\u8be6\u7ec6\u7684\u5f02\u5e38\u4fe1\u606f\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7528Python\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528os\u6a21\u5757\u548cpathlib\u6a21\u5757\u3002<strong>os\u6a21\u5757\u7b80\u5355\u6613\u7528\u3001\u517c\u5bb9\u6027\u597d<\/strong>\uff0c\u800c<strong>pathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u63a5\u53e3\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb<\/strong>\u3002\u6b64\u5916\uff0cshutil\u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u76ee\u5f55\uff0c\u4f46\u5b83\u4e3b\u8981\u7528\u4e8e\u9ad8\u7ea7\u6587\u4ef6\u64cd\u4f5c\u3002\u5728\u5904\u7406\u591a\u7ea7\u76ee\u5f55\u548c\u8def\u5f84\u5206\u9694\u7b26\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.makedirs()<\/code>\u548c<code>Path.mkdir(parents=True)<\/code>\u3002\u826f\u597d\u7684\u5f02\u5e38\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8c03\u8bd5\u6027\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u548c\u793a\u4f8b\u4ee3\u7801\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u521b\u5efa\u591a\u4e2a\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u7ed3\u5408<code>os<\/code>\u6a21\u5757\u6765\u521b\u5efa\u591a\u4e2a\u6587\u4ef6\u5939\u3002\u53ef\u4ee5\u4f7f\u7528<code>os.makedirs()<\/code>\u51fd\u6570\uff0c\u5728\u6307\u5b9a\u7684\u8def\u5f84\u4e0b\u521b\u5efa\u591a\u4e2a\u6587\u4ef6\u5939\uff0c\u5305\u62ec\u4efb\u4f55\u5fc5\u8981\u7684\u7236\u6587\u4ef6\u5939\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfolders = [&quot;folder1&quot;, &quot;folder2&quot;, &quot;folder3&quot;]\nfor folder in folders:\n    os.makedirs(folder, exist_ok=True)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e09\u4e2a\u6587\u4ef6\u5939\uff0c\u5982\u679c\u6587\u4ef6\u5939\u5df2\u5b58\u5728\uff0c\u5219\u4e0d\u4f1a\u62a5\u9519\u3002<\/p>\n<p><strong>\u5728\u521b\u5efa\u6587\u4ef6\u5939\u65f6\u5982\u4f55\u5904\u7406\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u6587\u4ef6\u5939\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u4e0d\u8db3\u6216\u8def\u5f84\u65e0\u6548\u7684\u60c5\u51b5\u3002\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\ntry:\n    os.makedirs(&quot;new_folder&quot;)\nexcept OSError as e:\n    print(f&quot;\u521b\u5efa\u6587\u4ef6\u5939\u65f6\u51fa\u9519: {e}&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\u4e0d\u4f1a\u5d29\u6e83\uff0c\u5e76\u80fd\u591f\u8f93\u51fa\u5177\u4f53\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u521b\u5efa\u5e26\u6709\u7279\u5b9a\u6743\u9650\u7684\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u521b\u5efa\u6587\u4ef6\u5939\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>os.chmod()<\/code>\u51fd\u6570\u8bbe\u7f6e\u7279\u5b9a\u7684\u6743\u9650\u3002\u4f8b\u5982\uff0c\u8981\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\u5e76\u5c06\u5176\u6743\u9650\u8bbe\u7f6e\u4e3a\u53ea\u8bfb\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nimport stat\n\nfolder_name = &quot;protected_folder&quot;\nos.makedirs(folder_name, exist_ok=True)\nos.chmod(folder_name, stat.S_IREAD)  # \u53ea\u8bfb\u6743\u9650\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u521b\u5efa\u7684\u6587\u4ef6\u5939\u5728\u64cd\u4f5c\u7cfb\u7edf\u5c42\u9762\u4e0a\u5177\u6709\u6240\u9700\u7684\u8bbf\u95ee\u6743\u9650\u8bbe\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001\u6216\u8005shutil\u6a21\u5757\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\u3001\u6613 [&hellip;]","protected":false},"author":3,"featured_media":1125175,"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\/1125169"}],"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=1125169"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125169\/revisions"}],"predecessor-version":[{"id":1125179,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125169\/revisions\/1125179"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1125175"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1125169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1125169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1125169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}