{"id":1028303,"date":"2024-12-31T10:58:21","date_gmt":"2024-12-31T02:58:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1028303.html"},"modified":"2024-12-31T10:58:23","modified_gmt":"2024-12-31T02:58:23","slug":"%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8bpython%e7%9a%84io%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\/1028303.html","title":{"rendered":"\u5982\u4f55\u67e5\u770bpython\u7684io\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\/af6b407a-bfb5-4ce5-ab64-1ff56e3cf538.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u67e5\u770bpython\u7684io\u6587\u4ef6\u662f\u5426\u5b58\u5728\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u67e5\u770b\u6587\u4ef6\u662f\u5426\u5b58\u5728\u7684\u65b9\u6cd5\u6709\u5f88\u591a<\/strong>\uff0c<strong>\u5176\u4e2d\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os.path\u6a21\u5757\u3001pathlib\u6a21\u5757\u548ctry-except\u8bed\u53e5<\/strong>\u3002\u8fd9\u51e0\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u5c55\u5f00\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528os.path\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p><strong>os.path\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3aexists()\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u68c0\u67e5\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728<\/strong>\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0cTrue\u8868\u793a\u5b58\u5728\uff0cFalse\u8868\u793a\u4e0d\u5b58\u5728\u3002\u4f7f\u7528\u8be5\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001os.path\u6a21\u5757<\/h3>\n<\/p>\n<p><p><strong>os.path\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7528\u4e8e\u64cd\u4f5c\u8def\u5f84\u7684\u51fd\u6570\uff0c\u5176\u4e2dos.path.exists()\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728<\/strong>\u3002\u8be5\u65b9\u6cd5\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f20\u5165\u8981\u68c0\u67e5\u7684\u8def\u5f84\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def check_file_exists(file_path):<\/p>\n<p>    return os.path.exists(file_path)<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if check_file_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><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86os\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570check_file_exists()\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4f7f\u7528os.path.exists()\u65b9\u6cd5\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u6700\u540e\uff0c\u6211\u4eec\u8c03\u7528\u8be5\u51fd\u6570\u5e76\u6839\u636e\u8fd4\u56de\u503c\u8f93\u51fa\u76f8\u5e94\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ol>\n<li><strong>\u7b80\u5355\u6613\u7528<\/strong>\uff1aos.path.exists()\u65b9\u6cd5\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f20\u5165\u8def\u5f84\u5373\u53ef\u3002<\/li>\n<li><strong>\u517c\u5bb9\u6027\u597d<\/strong>\uff1aos.path\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\uff0c\u4e14\u517c\u5bb9\u6027\u597d\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u64cd\u4f5c\u7cfb\u7edf\u3002<\/li>\n<\/ol>\n<hr>\n<p><h3>\u4e8c\u3001pathlib\u6a21\u5757<\/h3>\n<\/p>\n<p><p><strong>pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u4e2a\u7528\u4e8e\u5904\u7406\u8def\u5f84\u7684\u6a21\u5757\uff0c\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5<\/strong>\u3002\u76f8\u6bd4\u4e8eos.path\u6a21\u5757\uff0cpathlib\u6a21\u5757\u7684\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def check_file_exists(file_path):<\/p>\n<p>    return Path(file_path).exists()<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if check_file_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><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86pathlib\u6a21\u5757\u4e2d\u7684Path\u7c7b\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570check_file_exists()\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4f7f\u7528Path\u7c7b\u7684exists()\u65b9\u6cd5\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002\u6700\u540e\uff0c\u6211\u4eec\u8c03\u7528\u8be5\u51fd\u6570\u5e76\u6839\u636e\u8fd4\u56de\u503c\u8f93\u51fa\u76f8\u5e94\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ol>\n<li><strong>\u9762\u5411\u5bf9\u8c61<\/strong>\uff1apathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u8def\u5f84\u64cd\u4f5c\u65b9\u6cd5\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/li>\n<li><strong>\u529f\u80fd\u5f3a\u5927<\/strong>\uff1apathlib\u6a21\u5757\u9664\u4e86\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u5b9e\u7528\u7684\u65b9\u6cd5\uff0c\u5982\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u3001\u904d\u5386\u76ee\u5f55\u7b49\u3002<\/li>\n<\/ol>\n<hr>\n<p><h3>\u4e09\u3001try-except\u8bed\u53e5<\/h3>\n<\/p>\n<p><p><strong>try-except\u8bed\u53e5\u662f\u4e00\u79cd\u7528\u4e8e\u5f02\u5e38\u5904\u7406\u7684\u8bed\u53e5\uff0c\u53ef\u4ee5\u7528\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/strong>\u3002\u5728\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u65f6\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u6253\u5f00\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u4f1a\u5f15\u53d1FileNotFoundError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def check_file_exists(file_path):<\/p>\n<p>    try:<\/p>\n<p>        with open(file_path, &#39;r&#39;):<\/p>\n<p>            return True<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        return False<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if check_file_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><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570check_file_exists()\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4f7f\u7528try-except\u8bed\u53e5\u5c1d\u8bd5\u6253\u5f00\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u5219\u8fd4\u56deTrue\uff1b\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u6355\u83b7FileNotFoundError\u5f02\u5e38\u5e76\u8fd4\u56deFalse\u3002\u6700\u540e\uff0c\u6211\u4eec\u8c03\u7528\u8be5\u51fd\u6570\u5e76\u6839\u636e\u8fd4\u56de\u503c\u8f93\u51fa\u76f8\u5e94\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ol>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1atry-except\u8bed\u53e5\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u5904\u7406\u5f02\u5e38<\/strong>\uff1a\u9664\u4e86\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u8fd8\u53ef\u4ee5\u5728\u6355\u83b7\u5f02\u5e38\u65f6\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\uff0c\u5982\u8bb0\u5f55\u65e5\u5fd7\u3001\u63d0\u793a\u7528\u6237\u7b49\u3002<\/li>\n<\/ol>\n<hr>\n<p><h3>\u56db\u3001filecmp\u6a21\u5757<\/h3>\n<\/p>\n<p><p><strong>filecmp\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u6bd4\u8f83\u6587\u4ef6\u548c\u76ee\u5f55<\/strong>\u3002\u867d\u7136filecmp\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u6bd4\u8f83\u6587\u4ef6\u5185\u5bb9\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import filecmp<\/p>\n<p>def check_file_exists(file_path1, file_path2):<\/p>\n<p>    try:<\/p>\n<p>        return filecmp.cmp(file_path1, file_path2)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        return False<\/p>\n<p>file_path1 = &#39;example.txt&#39;<\/p>\n<p>file_path2 = &#39;example_copy.txt&#39;<\/p>\n<p>if check_file_exists(file_path1, file_path2):<\/p>\n<p>    print(f&quot;The file {file_path1} and {file_path2} exist and are identical.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The file {file_path1} or {file_path2} does not exist or they are not identical.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86filecmp\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570check_file_exists()\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u6587\u4ef6\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4f7f\u7528filecmp.cmp()\u65b9\u6cd5\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u662f\u5426\u76f8\u540c\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\u4e14\u5185\u5bb9\u76f8\u540c\uff0c\u5219\u8fd4\u56deTrue\uff1b\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u5185\u5bb9\u4e0d\u540c\uff0c\u5219\u6355\u83b7FileNotFoundError\u5f02\u5e38\u5e76\u8fd4\u56deFalse\u3002\u6700\u540e\uff0c\u6211\u4eec\u8c03\u7528\u8be5\u51fd\u6570\u5e76\u6839\u636e\u8fd4\u56de\u503c\u8f93\u51fa\u76f8\u5e94\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ol>\n<li><strong>\u6bd4\u8f83\u6587\u4ef6\u5185\u5bb9<\/strong>\uff1a\u9664\u4e86\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u8fd8\u53ef\u4ee5\u6bd4\u8f83\u6587\u4ef6\u5185\u5bb9\u662f\u5426\u76f8\u540c\u3002<\/li>\n<li><strong>\u9002\u7528\u4e8e\u6bd4\u8f83\u4efb\u52a1<\/strong>\uff1afilecmp\u6a21\u5757\u9002\u7528\u4e8e\u9700\u8981\u6bd4\u8f83\u6587\u4ef6\u548c\u76ee\u5f55\u5185\u5bb9\u7684\u4efb\u52a1\u3002<\/li>\n<\/ol>\n<hr>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u67e5\u770b\u6587\u4ef6\u662f\u5426\u5b58\u5728\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ecos.path\u6a21\u5757\u3001pathlib\u6a21\u5757\u3001try-except\u8bed\u53e5\u548cfilecmp\u6a21\u5757\u3002\u6bcf\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>os.path\u6a21\u5757<\/strong>\uff1a\u7b80\u5355\u6613\u7528\uff0c\u517c\u5bb9\u6027\u597d\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002<\/li>\n<li><strong>pathlib\u6a21\u5757<\/strong>\uff1a\u9762\u5411\u5bf9\u8c61\uff0c\u4ee3\u7801\u7b80\u6d01\u6613\u8bfb\uff0c\u529f\u80fd\u5f3a\u5927\u3002<\/li>\n<li><strong>try-except\u8bed\u53e5<\/strong>\uff1a\u7075\u6d3b\u6027\u9ad8\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u5904\u7406\u5f02\u5e38\u3002<\/li>\n<li><strong>filecmp\u6a21\u5757<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u6bd4\u8f83\u6587\u4ef6\u548c\u76ee\u5f55\u5185\u5bb9\u7684\u4efb\u52a1\u3002<\/li>\n<\/ol>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u540c\u65f6\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5efa\u8bae\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u9075\u5faaPython\u7684\u6700\u4f73\u5b9e\u8df5\u548c\u7f16\u7801\u89c4\u8303\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\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>\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\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u7528\u4e8e\u4efb\u4f55\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7684\u5f02\u5e38\u5904\u7406\u6765\u786e\u8ba4\u6587\u4ef6\u5b58\u5728\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u5f02\u5e38\u5904\u7406\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u5c1d\u8bd5\u6253\u5f00\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u5f15\u53d1<code>FileNotFoundError<\/code>\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\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\u53ef\u4ee5\u5728\u5c1d\u8bd5\u8bbf\u95ee\u6587\u4ef6\u7684\u540c\u65f6\uff0c\u786e\u8ba4\u6587\u4ef6\u7684\u5b58\u5728\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u68c0\u67e5\u76ee\u5f55\u7684\u5b58\u5728\u6027\u4e0e\u68c0\u67e5\u6587\u4ef6\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path<\/code>\u6a21\u5757\u4e2d\u7684<code>isdir()<\/code>\u51fd\u6570\u3002\u4e0b\u9762\u662f\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\ndir_path = &#39;your_directory&#39;\nif os.path.isdir(dir_path):\n    print(&quot;\u76ee\u5f55\u5b58\u5728&quot;)\nelse:\n    print(&quot;\u76ee\u5f55\u4e0d\u5b58\u5728&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u7279\u522b\u9002\u5408\u9700\u8981\u9a8c\u8bc1\u76ee\u5f55\u7ed3\u6784\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u67e5\u770b\u6587\u4ef6\u662f\u5426\u5b58\u5728\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5176\u4e2d\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os.path\u6a21\u5757\u3001pathlib\u6a21\u5757\u548ct [&hellip;]","protected":false},"author":3,"featured_media":1028316,"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\/1028303"}],"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=1028303"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1028303\/revisions"}],"predecessor-version":[{"id":1028320,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1028303\/revisions\/1028320"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1028316"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1028303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1028303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1028303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}