{"id":1088432,"date":"2025-01-08T13:44:10","date_gmt":"2025-01-08T05:44:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1088432.html"},"modified":"2025-01-08T13:44:13","modified_gmt":"2025-01-08T05:44:13","slug":"mac-%e5%a6%82%e4%bd%95%e7%94%a8python%e5%86%99%e6%96%87%e4%bb%b6%e6%a0%bc%e5%bc%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1088432.html","title":{"rendered":"mac \u5982\u4f55\u7528python\u5199\u6587\u4ef6\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200914\/5f307f04-1ca6-4050-b2f6-14c89fb39a1f.webp\" alt=\"mac \u5982\u4f55\u7528python\u5199\u6587\u4ef6\u683c\u5f0f\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5728Mac\u4e0a\u5199\u6587\u4ef6\u7684\u683c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5728Mac\u4e0a\u4f7f\u7528Python\u5199\u6587\u4ef6\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u6253\u5f00\u6587\u4ef6\u3001\u5199\u5165\u5185\u5bb9\u3001\u5173\u95ed\u6587\u4ef6\u7b49\u3002\u8fd9\u4e9b\u6b65\u9aa4\u53ef\u4ee5\u901a\u8fc7Python\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\u7684\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6253\u5f00\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\u3002\u6b64\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6587\u4ef6\u540d\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6a21\u5f0f\u3002\u6a21\u5f0f\u5305\u62ec\u8bfb\u6a21\u5f0f(&#39;r&#39;)\u3001\u5199\u6a21\u5f0f(&#39;w&#39;)\u3001\u8ffd\u52a0\u6a21\u5f0f(&#39;a&#39;)\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&quot;example.txt&quot;, &quot;w&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5728\u5f53\u524d\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>example.txt<\/code>\u7684\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5199\u6a21\u5f0f\u5c06\u8986\u76d6\u6587\u4ef6\u5185\u5bb9\uff1b\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5199\u6a21\u5f0f\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5199\u5165\u5185\u5bb9<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.write(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u5b57\u7b26\u4e32&quot;Hello, World!&quot;\u5199\u5165\u6587\u4ef6\u4e2d\u3002\u5982\u679c\u9700\u8981\u5199\u5165\u591a\u884c\u6587\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u6362\u884c\u7b26<code>\\n<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.write(&quot;Hello, World!\\n&quot;)<\/p>\n<p>file.write(&quot;This is a new line.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9664\u4e86<code>write()<\/code>\u65b9\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>writelines()<\/code>\u65b9\u6cd5\u4e00\u6b21\u6027\u5199\u5165\u591a\u884c\u5185\u5bb9\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>writelines()<\/code>\u65b9\u6cd5\u4e0d\u4f1a\u81ea\u52a8\u6dfb\u52a0\u6362\u884c\u7b26\uff0c\u6240\u4ee5\u9700\u8981\u624b\u52a8\u5728\u6bcf\u884c\u672b\u5c3e\u6dfb\u52a0<code>\\n<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [&quot;Hello, World!\\n&quot;, &quot;This is a new line.\\n&quot;]<\/p>\n<p>file.writelines(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5173\u95ed\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u5199\u5165\u64cd\u4f5c\u540e\uff0c\u4f7f\u7528<code>close()<\/code>\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\u3002\u8fd9\u662f\u4e00\u4e2a\u826f\u597d\u7684\u7f16\u7a0b\u4e60\u60ef\uff0c\u6709\u52a9\u4e8e\u91ca\u653e\u7cfb\u7edf\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528with\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u8fd9\u6837\u5373\u4f7f\u5728\u51fa\u73b0\u5f02\u5e38\u7684\u60c5\u51b5\u4e0b\uff0c\u6587\u4ef6\u4e5f\u4f1a\u88ab\u6b63\u786e\u5173\u95ed\u3002<code>with<\/code>\u8bed\u53e5\u8fd8\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;Hello, World!\\n&quot;)<\/p>\n<p>    file.write(&quot;This is a new line.\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6<\/p>\n<\/p>\n<p><p>Python\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u6587\u672c\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u5199\u5165\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.bin&quot;, &quot;wb&quot;) as file:<\/p>\n<p>    file.write(b&#39;\\x00\\x01\\x02\\x03&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>wb<\/code>\u6a21\u5f0f\u7528\u4e8e\u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c<code>b&#39;&#39;<\/code>\u8868\u793a\u4e8c\u8fdb\u5236\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684\u9ad8\u7ea7\u529f\u80fd<\/p>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u6587\u4ef6\u5904\u7406\u529f\u80fd\u3002\u4f8b\u5982\uff0c<code>csv<\/code>\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5904\u7406CSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&quot;example.csv&quot;, &quot;w&quot;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerow([&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;])<\/p>\n<p>    writer.writerow([&quot;Alice&quot;, 30, &quot;New York&quot;])<\/p>\n<p>    writer.writerow([&quot;Bob&quot;, 25, &quot;Los Angeles&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\uff0c\u5176\u4e2d\u6bcf\u884c\u6570\u636e\u662f\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84<\/p>\n<\/p>\n<p><p>\u5728Mac\u4e0a\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684<code>os<\/code>\u6a21\u5757\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = os.path.join(os.path.expanduser(&quot;~&quot;), &quot;example.txt&quot;)<\/p>\n<p>with open(file_path, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f7f\u7528<code>os.path.join()<\/code>\u65b9\u6cd5\u6784\u5efa\u6587\u4ef6\u8def\u5f84\uff0c<code>os.path.expanduser(&quot;~&quot;)<\/code>\u65b9\u6cd5\u83b7\u53d6\u7528\u6237\u7684\u4e3b\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u5f02\u5e38\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u83b7\u548c\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>        file.write(&quot;Hello, World!&quot;)<\/p>\n<p>except IOError as e:<\/p>\n<p>    print(f&quot;An IOError occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6355\u83b7\u5e76\u5904\u7406<code>IOError<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Mac\u4e0a\u4f7f\u7528Python\u5199\u6587\u4ef6\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a\u6253\u5f00\u6587\u4ef6\u3001\u5199\u5165\u5185\u5bb9\u3001\u5173\u95ed\u6587\u4ef6\u3002\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684\u9ad8\u7ea7\u529f\u80fd\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u5904\u7406\u6587\u4ef6\u8def\u5f84\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\uff0c\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u57fa\u672c\u6280\u80fd\u548c\u9ad8\u7ea7\u529f\u80fd\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u3001\u53ef\u9760\u5730\u5904\u7406\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Mac\u4e0a\u4f7f\u7528Python\u521b\u5efa\u548c\u5199\u5165\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Mac\u4e0a\u4f7f\u7528Python\u521b\u5efa\u548c\u5199\u5165\u6587\u672c\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u6216\u8005\u6253\u5f00\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:\n    file.write(&#39;Hello, world!&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u6bb5\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>example.txt<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5199\u5165\u201cHello, world!\u201d\u8fd9\u53e5\u8bdd\u3002<\/p>\n<p><strong>\u5728Mac\u4e0a\u4f7f\u7528Python\u65f6\uff0c\u5982\u4f55\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u5c24\u5176\u662f\u5728\u6709\u591a\u8bed\u8a00\u5b57\u7b26\u7684\u60c5\u51b5\u4e0b\u3002\u5728\u6253\u5f00\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>encoding<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u7f16\u7801\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:\n    file.write(&#39;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u4ee5UTF-8\u7f16\u7801\u4fdd\u5b58\uff0c\u4ece\u800c\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5982\u679c\u9700\u8981\u5728Mac\u4e0a\u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u5e94\u8be5\u5982\u4f55\u64cd\u4f5c\uff1f<\/strong><br \/>\u5bf9\u4e8e\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u5982\u56fe\u50cf\u6216\u97f3\u9891\u6587\u4ef6\uff0c\u4f7f\u7528<code>&#39;wb&#39;<\/code>\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u662f\u5fc5\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5199\u5165\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">data = bytes([120, 3, 255, 0, 100])  # \u793a\u4f8b\u4e8c\u8fdb\u5236\u6570\u636e\nwith open(&#39;example.bin&#39;, &#39;wb&#39;) as file:\n    file.write(data)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u4ee5\u4e8c\u8fdb\u5236\u683c\u5f0f\u4fdd\u5b58\uff0c\u9002\u7528\u4e8e\u591a\u79cd\u6587\u4ef6\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5728Mac\u4e0a\u5199\u6587\u4ef6\u7684\u683c\u5f0f \u5728Mac\u4e0a\u4f7f\u7528Python\u5199\u6587\u4ef6\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u6253\u5f00\u6587\u4ef6\u3001\u5199\u5165\u5185\u5bb9\u3001\u5173 [&hellip;]","protected":false},"author":3,"featured_media":1088438,"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\/1088432"}],"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=1088432"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1088432\/revisions"}],"predecessor-version":[{"id":1088442,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1088432\/revisions\/1088442"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1088438"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1088432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1088432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1088432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}