{"id":1098484,"date":"2025-01-08T15:21:35","date_gmt":"2025-01-08T07:21:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098484.html"},"modified":"2025-01-08T15:21:37","modified_gmt":"2025-01-08T07:21:37","slug":"python%e5%a6%82%e4%bd%95%e6%89%be%e5%88%b0%e6%96%87%e4%bb%b6%e6%9c%80%e5%90%8e%e4%b8%80%e8%a1%8c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1098484.html","title":{"rendered":"python\u5982\u4f55\u627e\u5230\u6587\u4ef6\u6700\u540e\u4e00\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25062648\/224389c2-2746-4e9a-b658-117bf4b21b56.webp\" alt=\"python\u5982\u4f55\u627e\u5230\u6587\u4ef6\u6700\u540e\u4e00\u884c\" \/><\/p>\n<p><p> <strong>Python\u67e5\u627e\u6587\u4ef6\u6700\u540e\u4e00\u884c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u3001\u9010\u884c\u8bfb\u53d6\u3001\u4f7f\u7528seek\u51fd\u6570\u5b9a\u4f4d\u6587\u4ef6\u672b\u5c3e\u7b49\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528seek\u51fd\u6570\u5b9a\u4f4d\u6587\u4ef6\u672b\u5c3e\u5e76\u9010\u884c\u8bfb\u53d6\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528seek\u51fd\u6570\u5b9a\u4f4d\u6587\u4ef6\u672b\u5c3e\u5e76\u9010\u884c\u8bfb\u53d6<\/strong>\uff1a<\/p>\n<p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5e76\u67e5\u627e\u6700\u540e\u4e00\u884c\u7684\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\u3002\u4f7f\u7528Python\u7684<code>seek<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u5b9a\u4f4d\u6587\u4ef6\u7684\u672b\u5c3e\uff0c\u7136\u540e\u53cd\u5411\u8bfb\u53d6\u6587\u4ef6\uff0c\u76f4\u5230\u627e\u5230\u6700\u540e\u4e00\u884c\u7684\u6362\u884c\u7b26\u3002\u8fd9\u79cd\u65b9\u6cd5\u907f\u514d\u4e86\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5230\u5185\u5b58\u4e2d\u7684\u95ee\u9898\uff0c\u9002\u7528\u4e8e\u5927\u6587\u4ef6\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6587\u4ef6\u64cd\u4f5c\u57fa\u7840<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6587\u4ef6\u64cd\u4f5c\u662f\u975e\u5e38\u5e38\u89c1\u7684\u4efb\u52a1\u4e4b\u4e00\u3002\u6587\u4ef6\u64cd\u4f5c\u5305\u62ec\u6587\u4ef6\u7684\u6253\u5f00\u3001\u8bfb\u53d6\u3001\u5199\u5165\u548c\u5173\u95ed\u7b49\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u6587\u4ef6\uff1a\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u7684\u8def\u5f84\u548c\u6253\u5f00\u6a21\u5f0f\uff08\u5982\u8bfb\u6a21\u5f0f\u3001\u5199\u6a21\u5f0f\u7b49\uff09\u3002<\/li>\n<li>\u8bfb\u53d6\u6587\u4ef6\uff1a\u4f7f\u7528<code>read<\/code>\u3001<code>readline<\/code>\u6216<code>readlines<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/li>\n<li>\u5199\u5165\u6587\u4ef6\uff1a\u4f7f\u7528<code>write<\/code>\u6216<code>writelines<\/code>\u65b9\u6cd5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002<\/li>\n<li>\u5173\u95ed\u6587\u4ef6\uff1a\u4f7f\u7528<code>close<\/code>\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\uff0c\u91ca\u653e\u8d44\u6e90\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6\u793a\u4f8b<\/p>\n<p>file_path = &#39;example.txt&#39;<\/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><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528seek\u51fd\u6570\u5b9a\u4f4d\u6587\u4ef6\u672b\u5c3e<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5e76\u67e5\u627e\u6700\u540e\u4e00\u884c\u7684\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\u3002\u4f7f\u7528Python\u7684<code>seek<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u5b9a\u4f4d\u6587\u4ef6\u7684\u672b\u5c3e\uff0c\u7136\u540e\u53cd\u5411\u8bfb\u53d6\u6587\u4ef6\uff0c\u76f4\u5230\u627e\u5230\u6700\u540e\u4e00\u884c\u7684\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_last_line(file_path):<\/p>\n<p>    with open(file_path, &#39;rb&#39;) as file:<\/p>\n<p>        file.seek(-2, os.SEEK_END)  # \u5b9a\u4f4d\u5230\u6587\u4ef6\u672b\u5c3e\u5012\u6570\u7b2c\u4e8c\u4e2a\u5b57\u8282<\/p>\n<p>        while file.read(1) != b&#39;\\n&#39;:  # \u9010\u5b57\u8282\u8bfb\u53d6\uff0c\u76f4\u5230\u627e\u5230\u6362\u884c\u7b26<\/p>\n<p>            file.seek(-2, os.SEEK_CUR)  # \u5b9a\u4f4d\u5230\u524d\u4e00\u4e2a\u5b57\u8282<\/p>\n<p>        last_line = file.readline().decode()  # \u8bfb\u53d6\u6700\u540e\u4e00\u884c<\/p>\n<p>    return last_line<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>last_line = read_last_line(file_path)<\/p>\n<p>print(f&#39;Last line: {last_line}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u9010\u884c\u8bfb\u53d6\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5e76\u67e5\u627e\u6700\u540e\u4e00\u884c\u7684\u65b9\u6cd5\u4e5f\u5f88\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5e76\u67e5\u627e\u6700\u540e\u4e00\u884c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_last_line(file_path):<\/p>\n<p>    last_line = &#39;&#39;<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        for line in file:<\/p>\n<p>            last_line = line<\/p>\n<p>    return last_line<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>last_line = read_last_line(file_path)<\/p>\n<p>print(f&#39;Last line: {last_line}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5e93\u51fd\u6570\u7b80\u5316\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>Python\u7684\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u63d0\u4f9b\u4e86\u7b80\u5316\u6587\u4ef6\u64cd\u4f5c\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff0c<code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u8bfb\u53d6\u6587\u4ef6\u7684\u65b9\u4fbf\u65b9\u6cd5\uff0c<code>numpy<\/code>\u5e93\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>pandas<\/code>\u5e93\u8bfb\u53d6\u6587\u4ef6\u5e76\u67e5\u627e\u6700\u540e\u4e00\u884c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def read_last_line(file_path):<\/p>\n<p>    df = pd.read_csv(file_path)<\/p>\n<p>    last_line = df.iloc[-1]<\/p>\n<p>    return last_line<\/p>\n<p>file_path = &#39;example.csv&#39;<\/p>\n<p>last_line = read_last_line(file_path)<\/p>\n<p>print(f&#39;Last line: {last_line}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u5927\u6587\u4ef6\u7684\u4f18\u5316\u7b56\u7565<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u8003\u8651\u5185\u5b58\u548c\u6027\u80fd\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u7b56\u7565\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u751f\u6210\u5668\uff1a\u751f\u6210\u5668\u53ef\u4ee5\u5728\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u65f6\u8282\u7701\u5185\u5b58\u3002<\/li>\n<li>\u5206\u5757\u8bfb\u53d6\uff1a\u5206\u5757\u8bfb\u53d6\u6587\u4ef6\u53ef\u4ee5\u907f\u514d\u4e00\u6b21\u6027\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5230\u5185\u5b58\u4e2d\u3002<\/li>\n<li>\u4f7f\u7528\u5185\u5b58\u6620\u5c04\uff1a\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u53ef\u4ee5\u5c06\u6587\u4ef6\u7684\u4e00\u90e8\u5206\u6620\u5c04\u5230\u5185\u5b58\u4e2d\uff0c\u8fdb\u884c\u9ad8\u6548\u8bfb\u53d6\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def read_last_line(file_path):<\/p>\n<p>    with open(file_path, &#39;rb&#39;) as file:<\/p>\n<p>        file_size = os.path.getsize(file_path)<\/p>\n<p>        buffer_size = 1024<\/p>\n<p>        buffer = bytearray()<\/p>\n<p>        for offset in range(file_size, 0, -buffer_size):<\/p>\n<p>            file.seek(max(0, offset - buffer_size), os.SEEK_SET)<\/p>\n<p>            buffer.extend(file.read(min(buffer_size, offset)))<\/p>\n<p>            if b&#39;\\n&#39; in buffer:<\/p>\n<p>                break<\/p>\n<p>        last_line = buffer.split(b&#39;\\n&#39;)[-1].decode()<\/p>\n<p>    return last_line<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>last_line = read_last_line(file_path)<\/p>\n<p>print(f&#39;Last line: {last_line}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u67e5\u627e\u6587\u4ef6\u6700\u540e\u4e00\u884c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\u548c\u6027\u80fd\u3002\u4f7f\u7528<code>seek<\/code>\u51fd\u6570\u5b9a\u4f4d\u6587\u4ef6\u672b\u5c3e\u5e76\u9010\u884c\u8bfb\u53d6\u7684\u65b9\u6cd5\u662f\u5904\u7406\u5927\u6587\u4ef6\u7684\u9ad8\u6548\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u548c\u4f7f\u7528\u5e93\u51fd\u6570\u4e5f\u53ef\u4ee5\u7b80\u5316\u6587\u4ef6\u64cd\u4f5c\u3002\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u8003\u8651\u5185\u5b58\u548c\u6027\u80fd\u95ee\u9898\uff0c\u53ef\u4ee5\u91c7\u7528\u751f\u6210\u5668\u3001\u5206\u5757\u8bfb\u53d6\u548c\u5185\u5b58\u6620\u5c04\u7b49\u4f18\u5316\u7b56\u7565\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u67e5\u627e\u6587\u4ef6\u6700\u540e\u4e00\u884c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6\u6587\u4ef6\u7684\u6700\u540e\u4e00\u884c\uff1f<\/strong><br \/>\u8981\u8bfb\u53d6\u6587\u4ef6\u7684\u6700\u540e\u4e00\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6\u7684\u6240\u6709\u884c\uff0c\u7136\u540e\u9009\u62e9\u6700\u540e\u4e00\u884c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;filename.txt&#39;, &#39;r&#39;) as file:\n    last_line = file.readlines()[-1]\nprint(last_line)\n<\/code><\/pre>\n<p>\u53e6\u4e00\u79cd\u66f4\u9ad8\u6548\u7684\u65b9\u6cd5\u662f\u4ece\u6587\u4ef6\u672b\u5c3e\u5f00\u59cb\u53cd\u5411\u8bfb\u53d6\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u52a0\u8f7d\u6574\u4e2a\u6587\u4ef6\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<p><strong>\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\uff0c\u6709\u4ec0\u4e48\u9ad8\u6548\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u6700\u540e\u4e00\u884c\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u5efa\u8bae\u4f7f\u7528\u6587\u4ef6\u7684seek()\u548ctell()\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u4ece\u6587\u4ef6\u672b\u5c3e\u5f00\u59cb\u53cd\u5411\u8bfb\u53d6\uff0c\u76f4\u5230\u627e\u5230\u6362\u884c\u7b26\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;filename.txt&#39;, &#39;rb&#39;) as file:\n    file.seek(0, 2)  # \u79fb\u52a8\u5230\u6587\u4ef6\u672b\u5c3e\n    pointer_location = file.tell()  # \u8bb0\u5f55\u5f53\u524d\u4f4d\u7f6e\n    while pointer_location &gt;= 0:\n        file.seek(pointer_location)\n        if file.read(1) == b&#39;\\n&#39; and pointer_location != pointer_location:\n            break\n        pointer_location -= 1\n    last_line = file.readline().decode()\nprint(last_line)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u6548\u7387\u8f83\u9ad8\uff0c\u9002\u7528\u4e8e\u5927\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u7a7a\u6587\u4ef6\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6\u6587\u4ef6\u6700\u540e\u4e00\u884c\u4e4b\u524d\uff0c\u5efa\u8bae\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\u4ee5\u53ca\u6587\u4ef6\u662f\u5426\u4e3a\u7a7a\u3002\u53ef\u4ee5\u4f7f\u7528os\u5e93\u6765\u68c0\u67e5\u6587\u4ef6\u7684\u5b58\u5728\u6027\uff0c\u4f7f\u7528\u7b80\u5355\u7684\u6761\u4ef6\u8bed\u53e5\u6765\u5224\u65ad\u6587\u4ef6\u7684\u5927\u5c0f\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nif os.path.exists(&#39;filename.txt&#39;) and os.path.getsize(&#39;filename.txt&#39;) &gt; 0:\n    # \u8bfb\u53d6\u6700\u540e\u4e00\u884c\u7684\u4ee3\u7801\nelse:\n    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u6587\u4ef6\u4e3a\u7a7a\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u907f\u514d\u5728\u5904\u7406\u6587\u4ef6\u65f6\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u67e5\u627e\u6587\u4ef6\u6700\u540e\u4e00\u884c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u3001\u9010\u884c\u8bfb\u53d6\u3001\u4f7f\u7528seek\u51fd\u6570\u5b9a\u4f4d\u6587\u4ef6\u672b\u5c3e\u7b49\u3002\u4e0b\u9762 [&hellip;]","protected":false},"author":3,"featured_media":1098492,"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\/1098484"}],"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=1098484"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098484\/revisions"}],"predecessor-version":[{"id":1098493,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098484\/revisions\/1098493"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098492"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}