{"id":1076370,"date":"2025-01-08T11:52:44","date_gmt":"2025-01-08T03:52:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1076370.html"},"modified":"2025-01-08T11:52:46","modified_gmt":"2025-01-08T03:52:46","slug":"python%e5%a6%82%e4%bd%95%e5%86%99if%e6%96%87%e4%bb%b6%e5%a4%b9-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1076370.html","title":{"rendered":"python\u5982\u4f55\u5199if\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\/24181001\/406ba139-055e-47ac-a67a-97cb9fb89b89.webp\" alt=\"python\u5982\u4f55\u5199if\u6587\u4ef6\u5939\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5982\u4f55\u5199if\u6587\u4ef6\u5939\u5b58\u5728<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u662f\u4e00\u9879\u5e38\u89c1\u7684\u4efb\u52a1\u3002<strong>\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>os.path.exists<\/code>\u3001<code>os.path.isdir<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/strong> \u5176\u4e2d\uff0c<code>os.path.exists<\/code>\u53ef\u4ee5\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u800c<code>os.path.isdir<\/code>\u53ef\u4ee5\u8fdb\u4e00\u6b65\u9a8c\u8bc1\u8def\u5f84\u662f\u5426\u662f\u4e00\u4e2a\u76ee\u5f55\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u7684\u8be6\u7ec6\u8bf4\u660e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>folder_path = &#39;path\/to\/your\/folder&#39;<\/p>\n<p>if os.path.exists(folder_path) and os.path.isdir(folder_path):<\/p>\n<p>    print(f&quot;The folder {folder_path} exists.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The folder {folder_path} does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u5728\u8fdb\u884c\u4efb\u4f55\u4e0e\u76ee\u5f55\u76f8\u5173\u7684\u64cd\u4f5c\u4e4b\u524d\uff0c\u5148\u9a8c\u8bc1\u76ee\u5f55\u662f\u5426\u5b58\u5728\u3002\u8fd9\u53ef\u4ee5\u907f\u514d\u7531\u4e8e\u8def\u5f84\u9519\u8bef\u800c\u5bfc\u81f4\u7684\u9519\u8bef\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u9c81\u68d2\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>os<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b9a\u4e49\u6587\u4ef6\u5939\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5b9a\u4e49\u8981\u68c0\u67e5\u7684\u6587\u4ef6\u5939\u8def\u5f84\u3002\u8def\u5f84\u53ef\u4ee5\u662f\u7edd\u5bf9\u8def\u5f84\uff0c\u4e5f\u53ef\u4ee5\u662f\u76f8\u5bf9\u8def\u5f84\u3002\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u4f20\u9012\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">folder_path = &#39;path\/to\/your\/folder&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>os.path.exists<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>os.path.exists<\/code>\u51fd\u6570\u7528\u4e8e\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff1a\u5982\u679c\u8def\u5f84\u5b58\u5728\uff0c\u5219\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002\u5728\u68c0\u67e5\u6587\u4ef6\u5939\u5b58\u5728\u65f6\uff0c\u9996\u5148\u4f7f\u7528\u6b64\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if os.path.exists(folder_path):<\/p>\n<p>    print(f&quot;The path {folder_path} exists.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The path {folder_path} does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>os.path.isdir<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1<code>os.path.exists<\/code>\u51fd\u6570\u53ef\u4ee5\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u4f46\u5b83\u5e76\u4e0d\u4f1a\u533a\u5206\u8def\u5f84\u662f\u6587\u4ef6\u8fd8\u662f\u6587\u4ef6\u5939\u3002\u8981\u786e\u4fdd\u8def\u5f84\u662f\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u4f7f\u7528<code>os.path.isdir<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff1a\u5982\u679c\u8def\u5f84\u662f\u4e00\u4e2a\u76ee\u5f55\uff0c\u5219\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if os.path.exists(folder_path) and os.path.isdir(folder_path):<\/p>\n<p>    print(f&quot;The folder {folder_path} exists.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The folder {folder_path} does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u4e0d\u5b58\u5728\u7684\u6587\u4ef6\u5939<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6587\u4ef6\u5939\u4e0d\u5b58\u5728\uff0c\u53ef\u4ee5\u9009\u62e9\u521b\u5efa\u5b83\u3002<code>os.makedirs<\/code>\u51fd\u6570\u53ef\u4ee5\u9012\u5f52\u5730\u521b\u5efa\u76ee\u5f55\u6811\u3002\u8fd9\u6837\uff0c\u5373\u4f7f\u7236\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u4e5f\u4f1a\u81ea\u52a8\u521b\u5efa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if not os.path.exists(folder_path):<\/p>\n<p>    os.makedirs(folder_path)<\/p>\n<p>    print(f&quot;The folder {folder_path} was created.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The folder {folder_path} already exists.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u901a\u5e38\u662f\u6587\u4ef6\u64cd\u4f5c\u7684\u7b2c\u4e00\u6b65\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u68c0\u67e5\u6587\u4ef6\u5939\u5b58\u5728\u540e\uff0c\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>folder_path = &#39;path\/to\/your\/folder&#39;<\/p>\n<p>file_path = os.path.join(folder_path, &#39;example.txt&#39;)<\/p>\n<p>if os.path.exists(folder_path) and os.path.isdir(folder_path):<\/p>\n<p>    if os.path.exists(file_path):<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>            print(content)<\/p>\n<p>    else:<\/p>\n<p>        with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>            file.write(&#39;This is an example file.&#39;)<\/p>\n<p>            print(f&quot;File {file_path} was created.&quot;)<\/p>\n<p>else:<\/p>\n<p>    os.makedirs(folder_path)<\/p>\n<p>    with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>        file.write(&#39;This is an example file.&#39;)<\/p>\n<p>        print(f&quot;Folder {folder_path} and file {file_path} were created.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728\u68c0\u67e5\u6587\u4ef6\u5939\u5b58\u5728\u540e\uff0c\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u5939\u6216\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5c06\u81ea\u52a8\u521b\u5efa\u5b83\u4eec\u3002\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u4e86\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u5e76\u51cf\u5c11\u4e86\u9519\u8bef\u53d1\u751f\u7684\u53ef\u80fd\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u662f\u4e00\u4e2a\u7b80\u5355\u4f46\u91cd\u8981\u7684\u4efb\u52a1\u3002<strong>\u901a\u8fc7\u4f7f\u7528<code>os.path.exists<\/code>\u548c<code>os.path.isdir<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/strong> \u6b64\u5916\uff0c\u901a\u8fc7\u7ed3\u5408<code>os.makedirs<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u6587\u4ef6\u5939\u4e0d\u5b58\u5728\u65f6\u81ea\u52a8\u521b\u5efa\u5b83\u4eec\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002\u65e0\u8bba\u662f\u5728\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u8fd8\u662f\u5728\u5f00\u53d1\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u638c\u63e1\u8fd9\u4e9b\u57fa\u672c\u64cd\u4f5c\u90fd\u662f\u975e\u5e38\u6709\u7528\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6765\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u3002\u4f7f\u7528<code>os.path.exists()<\/code>\u51fd\u6570\u53ef\u4ee5\u5224\u65ad\u6307\u5b9a\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u662f\u6587\u4ef6\u5939\u5219\u8fd4\u56de<code>True<\/code>\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfolder_path = &#39;your_folder_path&#39;\nif os.path.exists(folder_path) and os.path.isdir(folder_path):\n    print(&quot;\u6587\u4ef6\u5939\u5b58\u5728&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u5939\u4e0d\u5b58\u5728&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u6709\u6548\u5730\u786e\u8ba4\u6587\u4ef6\u5939\u7684\u5b58\u5728\u72b6\u6001\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u521b\u5efa\u65b0\u6587\u4ef6\u5939\u53ef\u4ee5\u4f7f\u7528<code>os.makedirs()<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u521b\u5efa\u5355\u4e2a\u6587\u4ef6\u5939\uff0c\u8fd8\u53ef\u4ee5\u521b\u5efa\u591a\u5c42\u5d4c\u5957\u6587\u4ef6\u5939\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nnew_folder_path = &#39;new_folder&#39;\nif not os.path.exists(new_folder_path):\n    os.makedirs(new_folder_path)\n    print(&quot;\u6587\u4ef6\u5939\u521b\u5efa\u6210\u529f&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u5939\u5df2\u5b58\u5728&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u7ba1\u7406\u6587\u4ef6\u5939\u7684\u521b\u5efa\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u5220\u9664\u4e00\u4e2a\u7a7a\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u82e5\u8981\u5220\u9664\u7a7a\u6587\u4ef6\u5939\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.rmdir()<\/code>\u51fd\u6570\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u4e2a\u51fd\u6570\u53ea\u80fd\u5220\u9664\u7a7a\u6587\u4ef6\u5939\uff0c\u5982\u679c\u6587\u4ef6\u5939\u5185\u6709\u6587\u4ef6\u6216\u5176\u4ed6\u6587\u4ef6\u5939\uff0c\u5219\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfolder_to_remove = &#39;folder_to_remove&#39;\nif os.path.isdir(folder_to_remove):\n    os.rmdir(folder_to_remove)\n    print(&quot;\u6587\u4ef6\u5939\u5df2\u6210\u529f\u5220\u9664&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u5939\u4e0d\u5b58\u5728\u6216\u4e0d\u4e3a\u7a7a&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u4e86\u6587\u4ef6\u5939\u7684\u5b89\u5168\u5220\u9664\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5982\u4f55\u5199if\u6587\u4ef6\u5939\u5b58\u5728 \u5728Python\u7f16\u7a0b\u4e2d\uff0c\u68c0\u67e5\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\u662f\u4e00\u9879\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4f7f\u7528os\u6a21\u5757\u4e2d\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1076375,"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\/1076370"}],"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=1076370"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076370\/revisions"}],"predecessor-version":[{"id":1076376,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076370\/revisions\/1076376"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1076375"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1076370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1076370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1076370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}