{"id":1141224,"date":"2025-01-08T22:29:02","date_gmt":"2025-01-08T14:29:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141224.html"},"modified":"2025-01-08T22:29:05","modified_gmt":"2025-01-08T14:29:05","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e6%96%b0%e5%bb%ba%e4%b8%80%e4%b8%aa%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1141224.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528python\u65b0\u5efa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104004\/3a1c5681-afc5-42e5-b090-63708dfd7fb6.webp\" alt=\"\u5982\u4f55\u4f7f\u7528python\u65b0\u5efa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u65b0\u5efa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528\u5185\u7f6e\u7684<code>open<\/code>\u51fd\u6570\u5373\u53ef\u5b8c\u6210\u3002<\/strong>\u3001<strong>\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\u6765\u521b\u5efa\u6587\u4ef6<\/strong>\u3001<strong>\u53ef\u4ee5\u5229\u7528<code>with<\/code>\u8bed\u53e5\u6765\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed<\/strong>\u3002\u4ee5\u4e0b\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u70b9\uff1a<strong>\u53ef\u4ee5\u5229\u7528<code>with<\/code>\u8bed\u53e5\u6765\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed<\/strong>\u3002\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u5b8c\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>example.txt<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u5b57\u7b26\u4e32&quot;Hello, World!&quot;\u5199\u5165\u5176\u4e2d\u3002\u65e0\u9700\u663e\u5f0f\u8c03\u7528<code>file.close()<\/code>\uff0c\u56e0\u4e3a<code>with<\/code>\u8bed\u53e5\u4f1a\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u5173\u95ed\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u57fa\u672c\u6587\u4ef6\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1\u3001\u6253\u5f00\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u6765\u6253\u5f00\u6587\u4ef6\u3002<code>open<\/code>\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;filename&#39;, &#39;mode&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>filename<\/strong>: \u6587\u4ef6\u7684\u540d\u79f0\u3002<\/li>\n<li><strong>mode<\/strong>: \u6587\u4ef6\u7684\u6253\u5f00\u6a21\u5f0f\uff0c\u5982<code>&#39;r&#39;<\/code>\uff08\u8bfb\uff09\u3001<code>&#39;w&#39;<\/code>\uff08\u5199\uff09\u3001<code>&#39;a&#39;<\/code>\uff08\u8ffd\u52a0\uff09\u7b49\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;w&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5199\u5165\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>write<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.write(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5173\u95ed\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5173\u95ed\u6587\u4ef6\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u786e\u4fdd\u8d44\u6e90\u88ab\u6b63\u786e\u91ca\u653e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>with<\/code>\u8bed\u53e5<\/h3>\n<\/p>\n<p><h4>1\u3001\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90<\/h4>\n<\/p>\n<p><p><code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u907f\u514d\u624b\u52a8\u8c03\u7528<code>close<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u51cf\u5c11\u51fa\u9519\u673a\u4f1a<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u51cf\u5c11\u4ee3\u7801\u51fa\u9519\u7684\u673a\u4f1a\uff0c\u56e0\u4e3a\u5b83\u4f1a\u786e\u4fdd\u6587\u4ef6\u5728\u9000\u51fa\u4ee3\u7801\u5757\u65f6\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6587\u4ef6\u6a21\u5f0f\u8be6\u89e3<\/h3>\n<\/p>\n<p><h4>1\u3001\u8bfb\u6a21\u5f0f\uff08&#39;r&#39;\uff09<\/h4>\n<\/p>\n<p><p>\u6253\u5f00\u6587\u4ef6\u7528\u4e8e\u8bfb\u53d6\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5199\u6a21\u5f0f\uff08&#39;w&#39;\uff09<\/h4>\n<\/p>\n<p><p>\u6253\u5f00\u6587\u4ef6\u7528\u4e8e\u5199\u5165\u3002\u5982\u679c\u6587\u4ef6\u5df2\u7ecf\u5b58\u5728\uff0c\u6587\u4ef6\u4f1a\u88ab\u6e05\u7a7a\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8ffd\u52a0\u6a21\u5f0f\uff08&#39;a&#39;\uff09<\/h4>\n<\/p>\n<p><p>\u6253\u5f00\u6587\u4ef6\u7528\u4e8e\u8ffd\u52a0\u5185\u5bb9\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello ag<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff08&#39;b&#39;\uff09<\/h4>\n<\/p>\n<p><p>\u7528\u4e8e\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u5982\u56fe\u7247\u6216\u89c6\u9891\u3002\u53ef\u4ee5\u4e0e\u8bfb\u3001\u5199\u3001\u8ffd\u52a0\u6a21\u5f0f\u7ed3\u5408\u4f7f\u7528\uff0c\u5982<code>&#39;rb&#39;<\/code>\u3001<code>&#39;wb&#39;<\/code>\u3001<code>&#39;ab&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.bin&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    file.write(b&#39;Binary data&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6587\u4ef6\u64cd\u4f5c\u7684\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528<code>read<\/code>\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>readline<\/code>\u548c<code>readlines<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong><code>readline<\/code><\/strong>: \u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    line = file.readline()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong><code>readlines<\/code><\/strong>: \u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u884c\uff0c\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5199\u5165\u591a\u884c\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>writelines<\/code>\u65b9\u6cd5\u5c06\u4e00\u4e2a\u5217\u8868\u5199\u5165\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [&#39;Line 1\\n&#39;, &#39;Line 2\\n&#39;, &#39;Line 3\\n&#39;]<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.writelines(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u9010\u884c\u5199\u5165\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u9010\u884c\u5199\u5165\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [&#39;Line 1\\n&#39;, &#39;Line 2\\n&#39;, &#39;Line 3\\n&#39;]<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for line in lines:<\/p>\n<p>        file.write(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6587\u4ef6\u8def\u5f84\u5904\u7406<\/h3>\n<\/p>\n<p><h4>1\u3001\u7edd\u5bf9\u8def\u5f84\u4e0e\u76f8\u5bf9\u8def\u5f84<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7edd\u5bf9\u8def\u5f84<\/strong>: \u5305\u542b\u4ece\u6839\u76ee\u5f55\u5230\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\u3002<\/li>\n<li><strong>\u76f8\u5bf9\u8def\u5f84<\/strong>: \u76f8\u5bf9\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u8def\u5f84\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u7edd\u5bf9\u8def\u5f84<\/p>\n<p>with open(&#39;\/path\/to\/example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, World!&#39;)<\/p>\n<h2><strong>\u76f8\u5bf9\u8def\u5f84<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8de8\u5e73\u53f0\u8def\u5f84\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>path<\/code>\u529f\u80fd\u6765\u5904\u7406\u8de8\u5e73\u53f0\u7684\u6587\u4ef6\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>current_dir = os.getcwd()<\/p>\n<h2><strong>\u6784\u5efa\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = os.path.join(current_dir, &#39;example.txt&#39;)<\/p>\n<p>with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><h4>1\u3001\u6355\u83b7\u6587\u4ef6\u64cd\u4f5c\u4e2d\u7684\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>try...except<\/code>\u5757\u6765\u6355\u83b7\u6587\u4ef6\u64cd\u4f5c\u4e2d\u7684\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&#39;\u6587\u4ef6\u672a\u627e\u5230&#39;)<\/p>\n<p>except IOError:<\/p>\n<p>    print(&#39;\u6587\u4ef6\u8bfb\u53d6\u9519\u8bef&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u786e\u4fdd\u6587\u4ef6\u88ab\u6b63\u786e\u5173\u95ed<\/h4>\n<\/p>\n<p><p>\u5373\u4f7f\u53d1\u751f\u5f02\u5e38\uff0c\u4e5f\u8981\u786e\u4fdd\u6587\u4ef6\u88ab\u6b63\u786e\u5173\u95ed\uff0c\u53ef\u4ee5\u7ed3\u5408<code>try...finally<\/code>\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = None<\/p>\n<p>try:<\/p>\n<p>    file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>    content = file.read()<\/p>\n<p>finally:<\/p>\n<p>    if file:<\/p>\n<p>        file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6587\u4ef6\u7f16\u7801<\/h3>\n<\/p>\n<p><h4>1\u3001\u6307\u5b9a\u6587\u4ef6\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u975eASCII\u5b57\u7b26\u65f6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    file.write(&#39;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\u4e5f\u53ef\u4ee5\u6307\u5b9a\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u6587\u4ef6\u5bf9\u8c61\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1\u3001\u6587\u4ef6\u5bf9\u8c61\u7684\u5e38\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<ul>\n<li><strong><code>read<\/code><\/strong>: \u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5185\u5bb9\u3002<\/li>\n<li><strong><code>readline<\/code><\/strong>: \u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\u3002<\/li>\n<li><strong><code>readlines<\/code><\/strong>: \u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u884c\uff0c\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002<\/li>\n<li><strong><code>write<\/code><\/strong>: \u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u3002<\/li>\n<li><strong><code>writelines<\/code><\/strong>: \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u5199\u5165\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001\u6587\u4ef6\u5bf9\u8c61\u7684\u5c5e\u6027<\/h4>\n<\/p>\n<ul>\n<li><strong><code>name<\/code><\/strong>: \u6587\u4ef6\u7684\u540d\u79f0\u3002<\/li>\n<li><strong><code>mode<\/code><\/strong>: \u6587\u4ef6\u7684\u6253\u5f00\u6a21\u5f0f\u3002<\/li>\n<li><strong><code>closed<\/code><\/strong>: \u6587\u4ef6\u662f\u5426\u5df2\u7ecf\u5173\u95ed\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    print(file.name)  # \u8f93\u51fa: example.txt<\/p>\n<p>    print(file.mode)  # \u8f93\u51fa: w<\/p>\n<p>    print(file.closed)  # \u8f93\u51fa: False<\/p>\n<p>print(file.closed)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u9ad8\u7ea7\u6587\u4ef6\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1\u3001\u6587\u4ef6\u6307\u9488\u5b9a\u4f4d<\/h4>\n<\/p>\n<p><p>\u6587\u4ef6\u6307\u9488\u53ef\u4ee5\u901a\u8fc7<code>seek<\/code>\u65b9\u6cd5\u5b9a\u4f4d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    file.seek(5)<\/p>\n<p>    content = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u83b7\u53d6\u6587\u4ef6\u5f53\u524d\u4f4d\u7f6e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>tell<\/code>\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u6307\u9488\u5f53\u524d\u4f4d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    position = file.tell()<\/p>\n<p>    print(position)  # \u8f93\u51fa\u6587\u4ef6\u6307\u9488\u5f53\u524d\u4f4d\u7f6e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u8bb2\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u65b0\u5efa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u4ee5\u53ca\u76f8\u5173\u7684\u6587\u4ef6\u64cd\u4f5c\u3002<strong>\u4f7f\u7528\u5185\u7f6e\u7684<code>open<\/code>\u51fd\u6570\u3001\u5584\u7528<code>with<\/code>\u8bed\u53e5\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90\u3001\u4e86\u89e3\u6587\u4ef6\u64cd\u4f5c\u7684\u5404\u79cd\u6a21\u5f0f\u548c\u65b9\u6cd5<\/strong>\uff0c\u8fd9\u4e9b\u90fd\u662f\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u6280\u80fd\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1Python\u6587\u4ef6\u64cd\u4f5c\uff0c\u4e3a\u4f60\u7684\u7f16\u7a0b\u4e4b\u8def\u63d0\u4f9b\u6709\u529b\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u672c\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\uff0c\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\u3002\u901a\u5e38\uff0c\u4f7f\u7528\u6a21\u5f0f<code>&#39;w&#39;<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5219\u4f1a\u8986\u76d6\u5b83\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;\u65b0\u6587\u4ef6.txt&#39;, &#39;w&#39;) as file:\n    file.write(&#39;\u8fd9\u662f\u4e00\u4e2a\u65b0\u7684\u6587\u672c\u6587\u4ef6\u3002&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201c\u65b0\u6587\u4ef6.txt\u201d\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u5199\u5165\u6307\u5b9a\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u521b\u5efa\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u662f\u5426\u53ef\u4ee5\u6dfb\u52a0\u5185\u5bb9\u800c\u4e0d\u662f\u8986\u76d6\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u4f7f\u7528\u6a21\u5f0f<code>&#39;a&#39;<\/code>\uff08\u8ffd\u52a0\u6a21\u5f0f\uff09\u53ef\u4ee5\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u5185\u5bb9\uff0c\u800c\u4e0d\u4f1a\u5220\u9664\u5df2\u6709\u5185\u5bb9\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;\u65b0\u6587\u4ef6.txt&#39;, &#39;a&#39;) as file:\n    file.write(&#39;\u8fd9\u662f\u8ffd\u52a0\u7684\u5185\u5bb9\u3002&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728\u5df2\u6709\u6587\u672c\u7684\u57fa\u7840\u4e0a\u6dfb\u52a0\u65b0\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5728\u521b\u5efa\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u7f16\u7801\u683c\u5f0f\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5728\u521b\u5efa\u6587\u672c\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u3002\u5e38\u89c1\u7684\u7f16\u7801\u683c\u5f0f\u5305\u62ec<code>&#39;utf-8&#39;<\/code>\u548c<code>&#39;gbk&#39;<\/code>\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;\u65b0\u6587\u4ef6.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:\n    file.write(&#39;\u8fd9\u662f\u4f7f\u7528UTF-8\u7f16\u7801\u7684\u65b0\u6587\u4ef6\u3002&#39;)\n<\/code><\/pre>\n<p>\u6307\u5b9a\u7f16\u7801\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4e0d\u540c\u7cfb\u7edf\u548c\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6b63\u786e\u8bfb\u53d6\uff0c\u7279\u522b\u662f\u5f53\u6587\u4ef6\u4e2d\u5305\u542b\u975eASCII\u5b57\u7b26\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u65b0\u5efa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570\u5373\u53ef\u5b8c\u6210\u3002\u3001\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\u6765\u521b\u5efa [&hellip;]","protected":false},"author":3,"featured_media":1141236,"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\/1141224"}],"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=1141224"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141224\/revisions"}],"predecessor-version":[{"id":1141238,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141224\/revisions\/1141238"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1141236"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1141224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1141224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1141224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}