{"id":1065833,"date":"2024-12-31T16:20:55","date_gmt":"2024-12-31T08:20:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1065833.html"},"modified":"2024-12-31T16:20:57","modified_gmt":"2024-12-31T08:20:57","slug":"python%e5%a6%82%e4%bd%95%e6%8c%87%e5%ae%9a%e6%96%87%e4%bb%b6%e7%9b%ae%e5%bd%95%e6%96%87%e4%bb%b6%e6%98%af%e5%90%a6%e5%ad%98%e5%9c%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1065833.html","title":{"rendered":"python\u5982\u4f55\u6307\u5b9a\u6587\u4ef6\u76ee\u5f55\u6587\u4ef6\u662f\u5426\u5b58\u5728"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/ba8be66d-ddfc-4b32-81ad-45d3e82d6d32.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u6307\u5b9a\u6587\u4ef6\u76ee\u5f55\u6587\u4ef6\u662f\u5426\u5b58\u5728\" \/><\/p>\n<p><p> <strong>Python\u6307\u5b9a\u6587\u4ef6\u76ee\u5f55\u6587\u4ef6\u662f\u5426\u5b58\u5728\u53ef\u4ee5\u901a\u8fc7<\/strong>os.path.exists<strong>\u3001<\/strong>os.path.isfile<strong>\u3001<\/strong>pathlib.Path.exists<strong>\u7b49\u65b9\u6cd5\u5b9e\u73b0\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecdos.path.exists\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u68c0\u67e5\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u65f6\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u4ee5\u901a\u8fc7os.path\u6a21\u5757\u4e2d\u7684\u5404\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u662fos.path.exists\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001os.path.exists<\/h3>\n<\/p>\n<p><p>os.path.exists\u662f\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u68c0\u67e5\u7279\u5b9a\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002\u5b83\u65e2\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u4e5f\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5b58\u5728\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0cTrue\u8868\u793a\u8def\u5f84\u5b58\u5728\uff0cFalse\u8868\u793a\u8def\u5f84\u4e0d\u5b58\u5728\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<h2><strong>\u5b9a\u4e49\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(file_path):<\/p>\n<p>    print(f&quot;The file {file_path} exists.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The file {file_path} does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001os.path.isfile<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u53ea\u60f3\u68c0\u67e5\u67d0\u4e2a\u8def\u5f84\u662f\u5426\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u800c\u4e0d\u5173\u5fc3\u5b83\u662f\u5426\u662f\u4e00\u4e2a\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528os.path.isfile\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56deTrue\u8868\u793a\u8def\u5f84\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u8fd4\u56deFalse\u8868\u793a\u8def\u5f84\u4e0d\u5b58\u5728\u6216\u4e0d\u662f\u6587\u4ef6\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<h2><strong>\u5b9a\u4e49\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u4e14\u662f\u4e00\u4e2a\u6587\u4ef6<\/strong><\/h2>\n<p>if os.path.isfile(file_path):<\/p>\n<p>    print(f&quot;The file {file_path} exists and is a file.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The file {file_path} does not exist or is not a file.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001os.path.isdir<\/h3>\n<\/p>\n<p><p>\u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u4f60\u53ea\u60f3\u68c0\u67e5\u67d0\u4e2a\u8def\u5f84\u662f\u5426\u662f\u4e00\u4e2a\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528os.path.isdir\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56deTrue\u8868\u793a\u8def\u5f84\u662f\u4e00\u4e2a\u76ee\u5f55\uff0c\u8fd4\u56deFalse\u8868\u793a\u8def\u5f84\u4e0d\u5b58\u5728\u6216\u4e0d\u662f\u76ee\u5f55\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<h2><strong>\u5b9a\u4e49\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>dir_path = &#39;example_directory&#39;<\/p>\n<h2><strong>\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5b58\u5728\u4e14\u662f\u4e00\u4e2a\u76ee\u5f55<\/strong><\/h2>\n<p>if os.path.isdir(dir_path):<\/p>\n<p>    print(f&quot;The directory {dir_path} exists and is a directory.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The directory {dir_path} does not exist or is not a directory.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001pathlib.Path.exists<\/h3>\n<\/p>\n<p><p>\u5728Python 3.4\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0cpathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u8def\u5f84\u5904\u7406\u65b9\u5f0f\u3002\u8fd9\u4e2a\u6a21\u5757\u7684Path\u7c7b\u7684exists\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\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<h2><strong>\u5b9a\u4e49\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = Path(&#39;example.txt&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if file_path.exists():<\/p>\n<p>    print(f&quot;The file {file_path} exists.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The file {file_path} does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001pathlib.Path.is_file<\/h3>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u4e2d\u4e5f\u6709\u4e00\u4e2ais_file\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u8def\u5f84\u662f\u5426\u662f\u4e00\u4e2a\u6587\u4ef6\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<h2><strong>\u5b9a\u4e49\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = Path(&#39;example.txt&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u4e14\u662f\u4e00\u4e2a\u6587\u4ef6<\/strong><\/h2>\n<p>if file_path.is_file():<\/p>\n<p>    print(f&quot;The file {file_path} exists and is a file.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The file {file_path} does not exist or is not a file.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001pathlib.Path.is_dir<\/h3>\n<\/p>\n<p><p>pathlib\u6a21\u5757\u4e2d\u4e5f\u6709\u4e00\u4e2ais_dir\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u8def\u5f84\u662f\u5426\u662f\u4e00\u4e2a\u76ee\u5f55\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<h2><strong>\u5b9a\u4e49\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>dir_path = Path(&#39;example_directory&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5b58\u5728\u4e14\u662f\u4e00\u4e2a\u76ee\u5f55<\/strong><\/h2>\n<p>if dir_path.is_dir():<\/p>\n<p>    print(f&quot;The directory {dir_path} exists and is a directory.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The directory {dir_path} does not exist or is not a directory.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7efc\u5408\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u6765\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5148\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u7136\u540e\u8fdb\u4e00\u6b65\u68c0\u67e5\u5b83\u662f\u6587\u4ef6\u8fd8\u662f\u76ee\u5f55\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u5e94\u7528\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from pathlib import Path<\/p>\n<h2><strong>\u5b9a\u4e49\u8def\u5f84<\/strong><\/h2>\n<p>path = &#39;example.txt&#39;<\/p>\n<h2><strong>\u4f7f\u7528os.path.exists\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>if os.path.exists(path):<\/p>\n<p>    print(f&quot;The path {path} exists.&quot;)<\/p>\n<p>    # \u8fdb\u4e00\u6b65\u68c0\u67e5\u662f\u6587\u4ef6\u8fd8\u662f\u76ee\u5f55<\/p>\n<p>    if os.path.isfile(path):<\/p>\n<p>        print(f&quot;The path {path} is a file.&quot;)<\/p>\n<p>    elif os.path.isdir(path):<\/p>\n<p>        print(f&quot;The path {path} is a directory.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The path {path} does not exist.&quot;)<\/p>\n<h2><strong>\u4f7f\u7528pathlib.Path.exists\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>path_obj = Path(path)<\/p>\n<p>if path_obj.exists():<\/p>\n<p>    print(f&quot;The path {path} exists.&quot;)<\/p>\n<p>    # \u8fdb\u4e00\u6b65\u68c0\u67e5\u662f\u6587\u4ef6\u8fd8\u662f\u76ee\u5f55<\/p>\n<p>    if path_obj.is_file():<\/p>\n<p>        print(f&quot;The path {path} is a file.&quot;)<\/p>\n<p>    elif path_obj.is_dir():<\/p>\n<p>        print(f&quot;The path {path} is a directory.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The path {path} does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u9519\u8bef\u5904\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u5373\u4f7f\u5728\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\u65f6\uff0c\u4e5f\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u95ee\u9898\u7b49\u5f02\u5e38\u60c5\u51b5\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6dfb\u52a0\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u4ee3\u7801\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<h2><strong>\u5b9a\u4e49\u8def\u5f84<\/strong><\/h2>\n<p>path = &#39;example.txt&#39;<\/p>\n<p>try:<\/p>\n<p>    # \u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728<\/p>\n<p>    if os.path.exists(path):<\/p>\n<p>        print(f&quot;The path {path} exists.&quot;)<\/p>\n<p>        # \u8fdb\u4e00\u6b65\u68c0\u67e5\u662f\u6587\u4ef6\u8fd8\u662f\u76ee\u5f55<\/p>\n<p>        if os.path.isfile(path):<\/p>\n<p>            print(f&quot;The path {path} is a file.&quot;)<\/p>\n<p>        elif os.path.isdir(path):<\/p>\n<p>            print(f&quot;The path {path} is a directory.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;The path {path} does not exist.&quot;)<\/p>\n<p>except PermissionError:<\/p>\n<p>    print(f&quot;Permission denied: {path}&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u65f6\uff0c\u6709\u65f6\u9700\u8981\u8003\u8651\u6027\u80fd\u95ee\u9898\u3002\u4e00\u822c\u6765\u8bf4\uff0cos.path\u6a21\u5757\u7684\u65b9\u6cd5\u548cpathlib\u6a21\u5757\u7684\u65b9\u6cd5\u5728\u6027\u80fd\u4e0a\u5dee\u522b\u4e0d\u5927\uff0c\u4f46\u5728\u7279\u5b9a\u573a\u666f\u4e0b\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u5fae\u5c0f\u7684\u5dee\u5f02\u3002\u5efa\u8bae\u6839\u636e\u5b9e\u9645\u9700\u6c42\u548c\u4ee3\u7801\u98ce\u683c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u5982\u4f55\u6307\u5b9a\u6587\u4ef6\u76ee\u5f55\u6587\u4ef6\u662f\u5426\u5b58\u5728\u7684\u65b9\u6cd5\uff0c\u5305\u62ecos.path.exists\u3001os.path.isfile\u3001os.path.isdir\u4ee5\u53capathlib.Path.exists\u3001pathlib.Path.is_file\u3001pathlib.Path.is_dir\u7b49\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5efa\u8bae\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6dfb\u52a0\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u4ee3\u7801\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u4f7f\u4f60\u5728\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u65f6\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u7279\u5b9a\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>exists()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u7279\u5b9a\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165<code>os<\/code>\u6a21\u5757\uff0c\u7136\u540e\u6784\u9020\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\uff0c\u6700\u540e\u8c03\u7528<code>os.path.exists()<\/code>\u6765\u9a8c\u8bc1\u6587\u4ef6\u7684\u5b58\u5728\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\nfile_path = &#39;path\/to\/your\/file.txt&#39;\nif os.path.exists(file_path):\n    print(&quot;\u6587\u4ef6\u5b58\u5728&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728&quot;)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u6211\u8be5\u5982\u4f55\u521b\u5efa\u65b0\u6587\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u65b0\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f7f\u7528<code>&#39;w&#39;<\/code>\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u5c06\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">file_path = &#39;path\/to\/your\/new_file.txt&#39;\nwith open(file_path, &#39;w&#39;) as file:\n    file.write(&quot;\u8fd9\u662f\u65b0\u6587\u4ef6\u7684\u5185\u5bb9&quot;)\n<\/code><\/pre>\n<p><strong>\u5728\u68c0\u67e5\u6587\u4ef6\u5b58\u5728\u6027\u65f6\uff0c\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u9664\u4e86<code>os<\/code>\u6a21\u5757\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>pathlib<\/code>\u5e93\uff0c\u8fd9\u662f\u4e00\u4e2a\u66f4\u73b0\u4ee3\u7684\u6587\u4ef6\u8def\u5f84\u5904\u7406\u5e93\u3002\u4f7f\u7528<code>Path<\/code>\u7c7b\u53ef\u4ee5\u66f4\u76f4\u89c2\u5730\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from pathlib import Path\n\nfile_path = Path(&#39;path\/to\/your\/file.txt&#39;)\nif file_path.exists():\n    print(&quot;\u6587\u4ef6\u5b58\u5728&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u6307\u5b9a\u6587\u4ef6\u76ee\u5f55\u6587\u4ef6\u662f\u5426\u5b58\u5728\u53ef\u4ee5\u901a\u8fc7os.path.exists\u3001os.path.isfile\u3001pat [&hellip;]","protected":false},"author":3,"featured_media":1065837,"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\/1065833"}],"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=1065833"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1065833\/revisions"}],"predecessor-version":[{"id":1065839,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1065833\/revisions\/1065839"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1065837"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1065833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1065833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1065833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}