{"id":1085798,"date":"2025-01-08T13:19:41","date_gmt":"2025-01-08T05:19:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1085798.html"},"modified":"2025-01-08T13:19:44","modified_gmt":"2025-01-08T05:19:44","slug":"%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e8%af%bb%e5%8f%96%e6%96%87%e4%bb%b6%e5%86%85%e5%ae%b9%e4%b8%ba%e7%a9%bapython-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1085798.html","title":{"rendered":"\u5982\u4f55\u5224\u65ad\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u4e3a\u7a7apython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195528\/dd2c75c8-5b84-46f1-afc2-27a02e9872ba.webp\" alt=\"\u5982\u4f55\u5224\u65ad\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u4e3a\u7a7apython\" \/><\/p>\n<p><p> <strong>\u5224\u65ad\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u4e3a\u7a7a\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u8bfb\u53d6\u6587\u4ef6\u540e\u68c0\u67e5\u5185\u5bb9\u662f\u5426\u4e3a\u7a7a\u3001\u4f7f\u7528os\u5e93\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u3001\u7528try-except\u6765\u5904\u7406\u6587\u4ef6\u8bfb\u53d6\u5f02\u5e38\u3001\u4f7f\u7528\u8def\u5f84\u5e93pathlib\u5224\u65ad\u6587\u4ef6\u5927\u5c0f<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u8bfb\u53d6\u6587\u4ef6\u540e\u68c0\u67e5\u5185\u5bb9\u662f\u5426\u4e3a\u7a7a<\/strong>\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u6709\u6548\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6\u7684\u5185\u5bb9\u540e\uff0c\u68c0\u67e5\u8be5\u5185\u5bb9\u662f\u5426\u4e3a\u7a7a\u5b57\u7b26\u4e32\u6765\u5224\u65ad\u6587\u4ef6\u662f\u5426\u4e3a\u7a7a\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><p>\u5f53\u6211\u4eec\u5728Python\u4e2d\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528open\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528read\u6216readlines\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u5982\u679c\u6587\u4ef6\u5185\u5bb9\u4e3a\u7a7a\uff0cread\u65b9\u6cd5\u5c06\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\uff0creadlines\u65b9\u6cd5\u5c06\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5217\u8868\u3002\u901a\u8fc7\u68c0\u67e5\u8fd9\u4e9b\u8fd4\u56de\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u5224\u65ad\u6587\u4ef6\u662f\u5426\u4e3a\u7a7a\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_file_empty(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>        if not content:<\/p>\n<p>            return True<\/p>\n<p>        else:<\/p>\n<p>            return False<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if is_file_empty(file_path):<\/p>\n<p>    print(f&quot;The file {file_path} is empty.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The file {file_path} is not empty.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u51fd\u6570is_file_empty\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u5982\u679c\u8bfb\u53d6\u5230\u7684\u5185\u5bb9\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u5219\u8be5\u6587\u4ef6\u4e3a\u7a7a\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u5e93\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5224\u65ad\u6587\u4ef6\u662f\u5426\u4e3a\u7a7a\u7684\u65b9\u6cd5\u662f\u4f7f\u7528os\u5e93\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u3002\u6587\u4ef6\u5927\u5c0f\u4e3a0\u5b57\u8282\u8868\u793a\u6587\u4ef6\u4e3a\u7a7a\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def is_file_empty(file_path):<\/p>\n<p>    return os.path.getsize(file_path) == 0<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if is_file_empty(file_path):<\/p>\n<p>    print(f&quot;The file {file_path} is empty.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The file {file_path} is not empty.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u51fd\u6570is_file_empty\u4f7f\u7528os.path.getsize\u51fd\u6570\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\uff0c\u5e76\u68c0\u67e5\u5176\u662f\u5426\u4e3a0\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7528try-except\u6765\u5904\u7406\u6587\u4ef6\u8bfb\u53d6\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6709\u65f6\u6587\u4ef6\u53ef\u80fd\u4e0d\u5b58\u5728\u6216\u65e0\u6cd5\u6253\u5f00\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u5224\u65ad\u6587\u4ef6\u662f\u5426\u4e3a\u7a7a\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_file_empty(file_path):<\/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>            if not content:<\/p>\n<p>                return True<\/p>\n<p>            else:<\/p>\n<p>                return False<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;The file {file_path} does not exist.&quot;)<\/p>\n<p>        return False<\/p>\n<p>    except IOError:<\/p>\n<p>        print(f&quot;An error occurred while reading the file {file_path}.&quot;)<\/p>\n<p>        return False<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if is_file_empty(file_path):<\/p>\n<p>    print(f&quot;The file {file_path} is empty.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The file {file_path} is not empty.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u51fd\u6570is_file_empty\u4f7f\u7528try-except\u5757\u6355\u83b7\u6587\u4ef6\u8bfb\u53d6\u5f02\u5e38\uff0c\u5e76\u6839\u636e\u6355\u83b7\u7684\u5f02\u5e38\u7c7b\u578b\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u8def\u5f84\u5e93pathlib\u5224\u65ad\u6587\u4ef6\u5927\u5c0f<\/h3>\n<\/p>\n<p><p>Python\u7684pathlib\u5e93\u63d0\u4f9b\u4e86\u66f4\u65b9\u4fbf\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pathlib\u5e93\u6765\u5224\u65ad\u6587\u4ef6\u662f\u5426\u4e3a\u7a7a\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def is_file_empty(file_path):<\/p>\n<p>    file = Path(file_path)<\/p>\n<p>    return file.stat().st_size == 0<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if is_file_empty(file_path):<\/p>\n<p>    print(f&quot;The file {file_path} is empty.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The file {file_path} is not empty.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u51fd\u6570is_file_empty\u4f7f\u7528pathlib.Path\u5bf9\u8c61\u83b7\u53d6\u6587\u4ef6\u7684\u72b6\u6001\u4fe1\u606f\uff0c\u5e76\u68c0\u67e5\u5176\u5927\u5c0f\u662f\u5426\u4e3a0\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u5224\u65ad<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u5224\u65ad\uff0c\u4ee5\u786e\u4fdd\u5224\u65ad\u7684\u51c6\u786e\u6027\u548c\u53ef\u9760\u6027\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from pathlib import Path<\/p>\n<p>def is_file_empty(file_path):<\/p>\n<p>    try:<\/p>\n<p>        file = Path(file_path)<\/p>\n<p>        if not file.exists():<\/p>\n<p>            print(f&quot;The file {file_path} does not exist.&quot;)<\/p>\n<p>            return False<\/p>\n<p>        if file.stat().st_size == 0:<\/p>\n<p>            return True<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>            if not content:<\/p>\n<p>                return True<\/p>\n<p>            else:<\/p>\n<p>                return False<\/p>\n<p>    except IOError:<\/p>\n<p>        print(f&quot;An error occurred while reading the file {file_path}.&quot;)<\/p>\n<p>        return False<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if is_file_empty(file_path):<\/p>\n<p>    print(f&quot;The file {file_path} is empty.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The file {file_path} is not empty.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u51fd\u6570is_file_empty\u7ed3\u5408\u4e86pathlib\u5e93\u548c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u7684\u65b9\u6cd5\uff0c\u5e76\u4f7f\u7528try-except\u5757\u6355\u83b7\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u5224\u65ad\u6587\u4ef6\u5185\u5bb9\u662f\u5426\u4e3a\u7a7a\u3002\u9009\u62e9\u9002\u5408\u81ea\u5df1\u5e94\u7528\u573a\u666f\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u5224\u65ad\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5224\u65ad\u7684\u51c6\u786e\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u68c0\u67e5\u6587\u4ef6\u5185\u5bb9\u7684\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u68c0\u67e5\u6587\u4ef6\u5185\u5bb9\u662f\u5426\u4e3a\u7a7a\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u6307\u9488\u548c\u8fed\u4ee3\u5668\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5224\u65ad\u662f\u5426\u4e3a\u7a7a\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u6587\u4ef6\u6307\u9488<\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u6307\u9488\uff08file pointer\uff09\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u5224\u65ad\u6587\u4ef6\u662f\u5426\u4e3a\u7a7a\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_file_empty(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        file.seek(0, os.SEEK_END)<\/p>\n<p>        if file.tell() == 0:<\/p>\n<p>            return True<\/p>\n<p>        else:<\/p>\n<p>            return False<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if is_file_empty(file_path):<\/p>\n<p>    print(f&quot;The file {file_path} is empty.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The file {file_path} is not empty.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u51fd\u6570is_file_empty\u4f7f\u7528seek\u65b9\u6cd5\u5c06\u6587\u4ef6\u6307\u9488\u79fb\u52a8\u5230\u6587\u4ef6\u672b\u5c3e\uff0c\u7136\u540e\u4f7f\u7528tell\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u6307\u9488\u7684\u4f4d\u7f6e\u3002\u5982\u679c\u4f4d\u7f6e\u4e3a0\uff0c\u5219\u8868\u793a\u6587\u4ef6\u4e3a\u7a7a\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u8fed\u4ee3\u5668<\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fed\u4ee3\u5668\u6765\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u5224\u65ad\u6587\u4ef6\u662f\u5426\u4e3a\u7a7a\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_file_empty(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        for line in file:<\/p>\n<p>            return False<\/p>\n<p>        return True<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>if is_file_empty(file_path):<\/p>\n<p>    print(f&quot;The file {file_path} is empty.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;The file {file_path} is not empty.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u51fd\u6570is_file_empty\u4f7f\u7528for\u5faa\u73af\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u5982\u679c\u8bfb\u53d6\u5230\u4efb\u4f55\u5185\u5bb9\uff0c\u5219\u8fd4\u56deFalse\uff0c\u5426\u5219\u8fd4\u56deTrue\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5224\u65ad\u6587\u4ef6\u5185\u5bb9\u662f\u5426\u4e3a\u7a7a\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u51e0\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u8bfb\u53d6\u6587\u4ef6\u540e\u68c0\u67e5\u5185\u5bb9\u662f\u5426\u4e3a\u7a7a\u3001\u4f7f\u7528os\u5e93\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u3001\u7528try-except\u5904\u7406\u6587\u4ef6\u8bfb\u53d6\u5f02\u5e38\u3001\u4f7f\u7528\u8def\u5f84\u5e93pathlib\u5224\u65ad\u6587\u4ef6\u5927\u5c0f\u3001\u4f7f\u7528\u6587\u4ef6\u6307\u9488\u548c\u8fed\u4ee3\u5668\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u5224\u65ad\uff0c\u4ee5\u63d0\u9ad8\u5224\u65ad\u7684\u51c6\u786e\u6027\u548c\u53ef\u9760\u6027\u3002\u540c\u65f6\uff0c\u5904\u7406\u6587\u4ef6\u8bfb\u53d6\u5f02\u5e38\u4e5f\u662f\u4fdd\u8bc1\u7a0b\u5e8f\u7a33\u5065\u6027\u7684\u91cd\u8981\u73af\u8282\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u901a\u8fc7\u5408\u7406\u7684\u4ee3\u7801\u7ed3\u6784\u548c\u6e05\u6670\u7684\u6ce8\u91ca\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002\u5e0c\u671b\u672c\u6587\u4ecb\u7ecd\u7684\u65b9\u6cd5\u5bf9\u4f60\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u6240\u5e2e\u52a9\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\u4e3a\u7a7a\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6253\u5f00\u6587\u4ef6\u5e76\u4f7f\u7528<code>read()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u5982\u679c\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u4e3a\u7a7a\uff0c\u90a3\u4e48\u6587\u4ef6\u5c31\u662f\u7a7a\u7684\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;filename.txt&#39;, &#39;r&#39;) as file:\n    content = file.read()\n    if not content:\n        print(&quot;\u6587\u4ef6\u4e3a\u7a7a&quot;)\n    else:\n        print(&quot;\u6587\u4ef6\u4e0d\u4e3a\u7a7a&quot;)\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528\u54ea\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u9ad8\u6548\u5730\u5224\u65ad\u6587\u4ef6\u5185\u5bb9\u662f\u5426\u4e3a\u7a7a\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>read()<\/code>\u65b9\u6cd5\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>stat()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u72b6\u6001\u4fe1\u606f\u3002\u901a\u8fc7\u68c0\u67e5\u6587\u4ef6\u7684\u5927\u5c0f\u662f\u5426\u4e3a\u96f6\uff0c\u53ef\u4ee5\u5224\u65ad\u6587\u4ef6\u662f\u5426\u4e3a\u7a7a\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import os\n\nif os.stat(&#39;filename.txt&#39;).st_size == 0:\n    print(&quot;\u6587\u4ef6\u4e3a\u7a7a&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u4e0d\u4e3a\u7a7a&quot;)\n<\/code><\/pre>\n<p><strong>\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u907f\u514d\u51fa\u73b0\u8bfb\u53d6\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u53ef\u4ee5\u5e2e\u52a9\u60a8\u907f\u514d\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u6587\u4ef6\u672a\u627e\u5230\u6216\u8bfb\u53d6\u9519\u8bef\u7b49\u5f02\u5e38\u60c5\u51b5\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    with open(&#39;filename.txt&#39;, &#39;r&#39;) as file:\n        content = file.read()\n        if not content:\n            print(&quot;\u6587\u4ef6\u4e3a\u7a7a&quot;)\nexcept FileNotFoundError:\n    print(&quot;\u6587\u4ef6\u672a\u627e\u5230\uff0c\u8bf7\u68c0\u67e5\u6587\u4ef6\u8def\u5f84&quot;)\nexcept Exception as e:\n    print(f&quot;\u8bfb\u53d6\u6587\u4ef6\u65f6\u53d1\u751f\u9519\u8bef: {e}&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5224\u65ad\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u4e3a\u7a7a\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u8bfb\u53d6\u6587\u4ef6\u540e\u68c0\u67e5\u5185\u5bb9\u662f\u5426\u4e3a\u7a7a\u3001\u4f7f\u7528os\u5e93\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u3001\u7528try-exc [&hellip;]","protected":false},"author":3,"featured_media":1085807,"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\/1085798"}],"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=1085798"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1085798\/revisions"}],"predecessor-version":[{"id":1085809,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1085798\/revisions\/1085809"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1085807"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1085798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1085798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1085798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}