{"id":1110480,"date":"2025-01-08T17:20:07","date_gmt":"2025-01-08T09:20:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1110480.html"},"modified":"2025-01-08T17:20:09","modified_gmt":"2025-01-08T09:20:09","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e4%b8%80%e4%b8%aa%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\/1110480.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6\u4e00\u4e2a\u6587\u4ef6\u662f\u5426\u5b58\u5728"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073311\/fff08e42-a56c-42fa-8523-f510b24079ef.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6\u4e00\u4e2a\u6587\u4ef6\u662f\u5426\u5b58\u5728\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6\u4e00\u4e2a\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u53ef\u4ee5\u4f7f\u7528os.path\u6a21\u5757\u4e2d\u7684isfile()\u65b9\u6cd5\u3001\u4f7f\u7528pathlib\u6a21\u5757\u4e2d\u7684Path.exists()\u65b9\u6cd5\u3001\u4f7f\u7528try-except\u8bed\u53e5\u5c1d\u8bd5\u6253\u5f00\u6587\u4ef6\u3002<\/strong>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\uff0c\u5e76\u89e3\u91ca\u5176\u5177\u4f53\u4f7f\u7528\u60c5\u51b5\u548c\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os.path\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684os\u6a21\u5757\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u65b9\u6cd5\uff0c\u800cos.path\u5b50\u6a21\u5757\u5219\u5305\u542b\u4e86\u591a\u79cd\u8def\u5f84\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002\u5176\u4e2d\uff0cos.path.isfile()\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u7ed9\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\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\">import os<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if os.path.isfile(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><h4>\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1aos.path.isfile()\u65b9\u6cd5\u4f7f\u7528\u7b80\u5355\uff0c\u4ee3\u7801\u76f4\u89c2\uff0c\u5bb9\u6613\u7406\u89e3\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u4ec5\u9002\u7528\u4e8e\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u4e0d\u80fd\u5224\u65ad\u76ee\u5f55\u662f\u5426\u5b58\u5728\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u4ecePython 3.4\u5f00\u59cb\uff0cpathlib\u6a21\u5757\u88ab\u5f15\u5165\u5230\u6807\u51c6\u5e93\u4e2d\uff0c\u8be5\u6a21\u5757\u4e3a\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u63a5\u53e3\u3002Path.exists()\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u8def\u5f84\uff08\u6587\u4ef6\u6216\u76ee\u5f55\uff09\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<p>file_path = Path(&#39;example.txt&#39;)<\/p>\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><h4>\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1apathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u52a0\u9762\u5411\u5bf9\u8c61\u7684\u63a5\u53e3\uff0c\u4f7f\u7528Path\u7c7b\u53ef\u4ee5\u66f4\u52a0\u65b9\u4fbf\u5730\u8fdb\u884c\u8def\u5f84\u64cd\u4f5c\uff0c\u4e14\u53ef\u4ee5\u5224\u65ad\u8def\u5f84\u662f\u6587\u4ef6\u8fd8\u662f\u76ee\u5f55\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u5728Python 3.4\u4e4b\u524d\u7684\u7248\u672c\u4e2d\u4e0d\u53ef\u7528\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528try-except\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528try-except\u8bed\u53e5\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u6253\u5f00\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u629b\u51faFileNotFoundError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &#39;example.txt&#39;<\/p>\n<p>try:<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        print(f&quot;The file {file_path} exists.&quot;)<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(f&quot;The file {file_path} does not exist.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u53ef\u4ee5\u540c\u65f6\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u5e76\u8fdb\u884c\u8bfb\u53d6\u64cd\u4f5c\uff0c\u9002\u5408\u9700\u8981\u7acb\u5373\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u4ee3\u7801\u8f83\u4e3a\u5197\u957f\uff0c\u4e0d\u9002\u7528\u4e8e\u4ec5\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u7684\u7b80\u5355\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u8be6\u7ec6\u63cf\u8ff0os.path.isfile()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>\u65b9\u6cd5\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>os.path.isfile()\u65b9\u6cd5\u662fos.path\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5224\u65ad\u7ed9\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\u662f\u5426\u5b58\u5728\u4e14\u662f\u4e00\u4e2a\u5e38\u89c4\u6587\u4ef6\u3002\u8be5\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0cTrue\u8868\u793a\u6587\u4ef6\u5b58\u5728\u4e14\u662f\u4e00\u4e2a\u5e38\u89c4\u6587\u4ef6\uff0cFalse\u8868\u793a\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u4e0d\u662f\u5e38\u89c4\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>os.path.isfile()\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u4e14\u4e0d\u9700\u8981\u8fdb\u4e00\u6b65\u8def\u5f84\u64cd\u4f5c\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5728\u6587\u4ef6\u64cd\u4f5c\u524d\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u4ee5\u907f\u514d\u5728\u540e\u7eed\u64cd\u4f5c\u4e2d\u51fa\u73b0\u9519\u8bef\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 check_file_existence(file_path):<\/p>\n<p>    &quot;&quot;&quot;\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u5e76\u8fd4\u56de\u7ed3\u679c&quot;&quot;&quot;<\/p>\n<p>    return os.path.isfile(file_path)<\/p>\n<h2><strong>\u6d4b\u8bd5\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if check_file_existence(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><h4>\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ul>\n<li>os.path.isfile()\u65b9\u6cd5\u4ec5\u9002\u7528\u4e8e\u5224\u65ad\u5e38\u89c4\u6587\u4ef6\uff0c\u4e0d\u9002\u7528\u4e8e\u5224\u65ad\u76ee\u5f55\u548c\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\uff08\u5982\u8bbe\u5907\u6587\u4ef6\u3001\u7ba1\u9053\u7b49\uff09\u3002<\/li>\n<li>\u6587\u4ef6\u8def\u5f84\u53ef\u4ee5\u662f\u7edd\u5bf9\u8def\u5f84\u6216\u76f8\u5bf9\u8def\u5f84\uff0cos.path.isfile()\u65b9\u6cd5\u4f1a\u6839\u636e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u89e3\u6790\u76f8\u5bf9\u8def\u5f84\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u8be6\u7ec6\u63cf\u8ff0pathlib.Path.exists()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>\u65b9\u6cd5\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>Path.exists()\u65b9\u6cd5\u662fpathlib\u6a21\u5757\u4e2d\u7684Path\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u7528\u4e8e\u5224\u65ad\u8def\u5f84\uff08\u6587\u4ef6\u6216\u76ee\u5f55\uff09\u662f\u5426\u5b58\u5728\u3002\u8be5\u65b9\u6cd5\u4f1a\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>\u4f7f\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>Path.exists()\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u8def\u5f84\u64cd\u4f5c\u4e14\u9700\u8981\u5224\u65ad\u8def\u5f84\u662f\u5426\u5b58\u5728\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5728\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u524d\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u4ee5\u907f\u514d\u5728\u540e\u7eed\u64cd\u4f5c\u4e2d\u51fa\u73b0\u9519\u8bef\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 check_path_existence(file_path):<\/p>\n<p>    &quot;&quot;&quot;\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\u5e76\u8fd4\u56de\u7ed3\u679c&quot;&quot;&quot;<\/p>\n<p>    path = Path(file_path)<\/p>\n<p>    return path.exists()<\/p>\n<h2><strong>\u6d4b\u8bd5\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if check_path_existence(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><h4>\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ul>\n<li>Path.exists()\u65b9\u6cd5\u9002\u7528\u4e8e\u5224\u65ad\u6587\u4ef6\u548c\u76ee\u5f55\u662f\u5426\u5b58\u5728\uff0c\u4e14\u53ef\u4ee5\u901a\u8fc7Path\u7c7b\u7684is_file()\u548cis_dir()\u65b9\u6cd5\u8fdb\u4e00\u6b65\u5224\u65ad\u8def\u5f84\u7c7b\u578b\u3002<\/li>\n<li>Path\u7c7b\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u8def\u5f84\u62fc\u63a5\u3001\u904d\u5386\u7b49\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001try-except\u8bed\u53e5\u7684\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><h4>\u65b9\u6cd5\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>try-except\u8bed\u53e5\u662fPython\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\u3002\u5728\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u7684\u573a\u666f\u4e2d\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u6253\u5f00\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u4f1a\u629b\u51faFileNotFoundError\u5f02\u5e38\uff0c\u901a\u8fc7\u6355\u83b7\u8be5\u5f02\u5e38\u6765\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>try-except\u8bed\u53e5\u9002\u7528\u4e8e\u9700\u8981\u5728\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u7684\u540c\u65f6\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5728\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u524d\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u4ee5\u907f\u514d\u5728\u8bfb\u53d6\u64cd\u4f5c\u4e2d\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def check_and_read_file(file_path):<\/p>\n<p>    &quot;&quot;&quot;\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u5e76\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9&quot;&quot;&quot;<\/p>\n<p>    try:<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>        return content<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        return None<\/p>\n<h2><strong>\u6d4b\u8bd5\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>file_content = check_and_read_file(file_path)<\/p>\n<p>if file_content is not None:<\/p>\n<p>    print(f&quot;The file {file_path} exists and its content is:\\n{file_content}&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><h4>\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ul>\n<li>try-except\u8bed\u53e5\u9002\u7528\u4e8e\u9700\u8981\u5728\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u7684\u540c\u65f6\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u5982\u679c\u4ec5\u9700\u8981\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u4f7f\u7528os.path.isfile()\u6216Path.exists()\u65b9\u6cd5\u66f4\u4e3a\u7b80\u6d01\u3002<\/li>\n<li>\u5728\u6355\u83b7FileNotFoundError\u5f02\u5e38\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u5176\u4ed6\u5f02\u5e38\u5904\u7406\u64cd\u4f5c\uff0c\u5982\u8bb0\u5f55\u65e5\u5fd7\u3001\u63d0\u793a\u7528\u6237\u7b49\u3002<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8bfb\u53d6\u4e00\u4e2a\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u53ef\u4ee5\u4f7f\u7528os.path.isfile()\u65b9\u6cd5\u3001pathlib.Path.exists()\u65b9\u6cd5\u548ctry-except\u8bed\u53e5\u4e09\u79cd\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>os.path.isfile()\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u4e14\u4e0d\u9700\u8981\u8fdb\u4e00\u6b65\u8def\u5f84\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/li>\n<li><strong>pathlib.Path.exists()\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u8def\u5f84\u64cd\u4f5c\u4e14\u9700\u8981\u5224\u65ad\u8def\u5f84\u662f\u5426\u5b58\u5728\u7684\u573a\u666f\uff0c\u63d0\u4f9b\u66f4\u52a0\u9762\u5411\u5bf9\u8c61\u7684\u63a5\u53e3\u3002<\/li>\n<li><strong>try-except\u8bed\u53e5<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5728\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u7684\u540c\u65f6\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u901a\u8fc7\u6355\u83b7FileNotFoundError\u5f02\u5e38\u6765\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/li>\n<\/ul>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u6ce8\u610f\u65b9\u6cd5\u7684\u9002\u7528\u8303\u56f4\u548c\u53ef\u80fd\u7684\u5f02\u5e38\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u4e00\u4e2a\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u7684<code>exists()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfile_path = &#39;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>\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8e\u6587\u4ef6\uff0c\u4e5f\u9002\u7528\u4e8e\u76ee\u5f55\u3002<\/p>\n<p><strong>\u4f7f\u7528try-except\u8bed\u53e5\u68c0\u67e5\u6587\u4ef6\u5b58\u5728\u6027\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u4f7f\u7528try-except\u8bed\u53e5\u53ef\u4ee5\u5728\u5c1d\u8bd5\u6253\u5f00\u6587\u4ef6\u65f6\u6355\u83b7\u5f02\u5e38\uff0c\u8fdb\u800c\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    with open(&#39;your_file.txt&#39;, &#39;r&#39;) as file:\n        print(&quot;\u6587\u4ef6\u5b58\u5728&quot;)\nexcept FileNotFoundError:\n    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u5728\u4e8e\uff0c\u9664\u4e86\u68c0\u67e5\u6587\u4ef6\u5b58\u5728\u6027\uff0c\u8fd8\u80fd\u5904\u7406\u5176\u4ed6\u53ef\u80fd\u7684\u9519\u8bef\uff0c\u5982\u6743\u9650\u95ee\u9898\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u9a8c\u8bc1\u6587\u4ef6\u7684\u5b58\u5728\u6027\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>os.path.exists()<\/code>\u548ctry-except\u8bed\u53e5\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u548c\u8def\u5f84\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from pathlib import Path\n\nfile_path = Path(&#39;your_file.txt&#39;)\nif file_path.is_file():\n    print(&quot;\u6587\u4ef6\u5b58\u5728&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728&quot;)\n<\/code><\/pre>\n<p><code>pathlib<\/code>\u4e0d\u4ec5\u53ef\u4ee5\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u8def\u5f84\u64cd\u4f5c\uff0c\u4fbf\u4e8e\u6587\u4ef6\u7ba1\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6\u4e00\u4e2a\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u53ef\u4ee5\u4f7f\u7528os.path\u6a21\u5757\u4e2d\u7684isfile()\u65b9\u6cd5\u3001\u4f7f\u7528pathlib\u6a21\u5757\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1110488,"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\/1110480"}],"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=1110480"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110480\/revisions"}],"predecessor-version":[{"id":1110492,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110480\/revisions\/1110492"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1110488"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1110480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1110480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1110480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}