{"id":1074752,"date":"2025-01-08T11:37:31","date_gmt":"2025-01-08T03:37:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1074752.html"},"modified":"2025-01-08T11:37:33","modified_gmt":"2025-01-08T03:37:33","slug":"python%e5%a6%82%e4%bd%95%e8%bf%bd%e5%8a%a0%e4%ba%8c%e8%bf%9b%e5%88%b6%e6%96%87%e4%bb%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1074752.html","title":{"rendered":"python\u5982\u4f55\u8ffd\u52a0\u4e8c\u8fdb\u5236\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104312\/67d97b0f-654a-4e1a-85d3-2f0708c7cc8d.webp\" alt=\"python\u5982\u4f55\u8ffd\u52a0\u4e8c\u8fdb\u5236\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python \u8ffd\u52a0\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528open\u51fd\u6570\u3001\u4f7f\u7528with\u8bed\u53e5\u3001\u4ee5\u53ca\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u9009\u62e9\u6b63\u786e\u7684\u6a21\u5f0f\uff08&#39;ab&#39;\uff09\u7b49<\/strong>\u3002\u4f7f\u7528open\u51fd\u6570\u6253\u5f00\u4e8c\u8fdb\u5236\u6587\u4ef6\u65f6\uff0c\u901a\u8fc7\u9009\u62e9\u6a21\u5f0f&#39;ab&#39;\u53ef\u4ee5\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528with\u8bed\u53e5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u88ab\u6b63\u786e\u5173\u95ed\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528open\u51fd\u6570\u548c&#39;ab&#39;\u6a21\u5f0f\u8ffd\u52a0\u4e8c\u8fdb\u5236\u6587\u4ef6<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8ffd\u52a0\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u5e38\u89c1\u65b9\u6cd5\u662f\u4f7f\u7528open\u51fd\u6570\u5e76\u9009\u62e9&#39;ab&#39;\u6a21\u5f0f\u3002&#39;ab&#39;\u6a21\u5f0f\u8868\u793a\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6\u5e76\u8ffd\u52a0\u4e8c\u8fdb\u5236\u6570\u636e<\/p>\n<p>with open(&#39;example.bin&#39;, &#39;ab&#39;) as file:<\/p>\n<p>    # \u5b9a\u4e49\u8981\u8ffd\u52a0\u7684\u4e8c\u8fdb\u5236\u6570\u636e<\/p>\n<p>    data_to_append = b&#39;\\x00\\x01\\x02\\x03&#39;<\/p>\n<p>    # \u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6<\/p>\n<p>    file.write(data_to_append)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>with open(&#39;example.bin&#39;, &#39;ab&#39;) as file<\/code>\u6253\u5f00\u6587\u4ef6\uff0c\u786e\u4fdd\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u6587\u4ef6\u88ab\u6b63\u786e\u5173\u95ed\u3002\u7136\u540e\uff0c\u5b9a\u4e49\u8981\u8ffd\u52a0\u7684\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u5e76\u4f7f\u7528<code>file.write(data_to_append)<\/code>\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u672b\u5c3e\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528open\u51fd\u6570\u8ffd\u52a0\u4e8c\u8fdb\u5236\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528open\u51fd\u6570\u662f\u8ffd\u52a0\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u6700\u57fa\u672c\u65b9\u6cd5\u3002\u901a\u8fc7\u9009\u62e9&#39;ab&#39;\u6a21\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u88ab\u8ffd\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\uff0c\u800c\u4e0d\u4f1a\u8986\u76d6\u6587\u4ef6\u4e2d\u5df2\u6709\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def append_binary_file(file_path, data):<\/p>\n<p>    with open(file_path, &#39;ab&#39;) as file:<\/p>\n<p>        file.write(data)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>file_path = &#39;example.bin&#39;<\/p>\n<p>data_to_append = b&#39;\\x00\\x01\\x02\\x03&#39;<\/p>\n<p>append_binary_file(file_path, data_to_append)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>append_binary_file<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u6587\u4ef6\u8def\u5f84\u548c\u8981\u8ffd\u52a0\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u4f5c\u4e3a\u53c2\u6570\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>with open(file_path, &#39;ab&#39;) as file<\/code>\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>file.write(data)<\/code>\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u672b\u5c3e\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002\u7136\u800c\uff0c\u5982\u679c\u9700\u8981\u66f4\u52a0\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528with\u8bed\u53e5\u786e\u4fdd\u6587\u4ef6\u5173\u95ed<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u88ab\u6b63\u786e\u5173\u95ed\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f7f\u7528with\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def append_binary_file_with_with(file_path, data):<\/p>\n<p>    with open(file_path, &#39;ab&#39;) as file:<\/p>\n<p>        file.write(data)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>file_path = &#39;example_with.bin&#39;<\/p>\n<p>data_to_append = b&#39;\\x04\\x05\\x06\\x07&#39;<\/p>\n<p>append_binary_file_with_with(file_path, data_to_append)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>append_binary_file_with_with<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4e0e\u524d\u4e00\u4e2a\u793a\u4f8b\u4e2d\u7684\u51fd\u6570\u7c7b\u4f3c\u3002\u552f\u4e00\u7684\u533a\u522b\u662f\u4f7f\u7528\u4e86with\u8bed\u53e5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528with\u8bed\u53e5\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u4e14\u4e0d\u9700\u8981\u663e\u5f0f\u5173\u95ed\u6587\u4ef6\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5927\u591a\u6570\u6587\u4ef6\u64cd\u4f5c\u573a\u666f\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5904\u7406\u5927\u6587\u4ef6\u548c\u5206\u5757\u5199\u5165<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u6570\u636e\u5206\u5757\u5199\u5165\u6587\u4ef6\uff0c\u4ee5\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u5927\u91cf\u6570\u636e\u5230\u5185\u5b58\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528\u8fed\u4ee3\u5668\u6216\u751f\u6210\u5668\u6765\u5206\u5757\u8bfb\u53d6\u6570\u636e\uff0c\u5e76\u9010\u5757\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def append_large_binary_file(file_path, data_generator, chunk_size=1024):<\/p>\n<p>    with open(file_path, &#39;ab&#39;) as file:<\/p>\n<p>        for chunk in data_generator(chunk_size):<\/p>\n<p>            file.write(chunk)<\/p>\n<p>def data_generator(chunk_size):<\/p>\n<p>    # \u793a\u4f8b\u751f\u6210\u5668\uff0c\u751f\u6210\u5206\u5757\u6570\u636e<\/p>\n<p>    for i in range(0, 10000, chunk_size):<\/p>\n<p>        yield b&#39;\\x08&#39; * chunk_size<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>file_path = &#39;example_large.bin&#39;<\/p>\n<p>append_large_binary_file(file_path, data_generator)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>append_large_binary_file<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u6587\u4ef6\u8def\u5f84\u3001\u6570\u636e\u751f\u6210\u5668\u548c\u5757\u5927\u5c0f\u4f5c\u4e3a\u53c2\u6570\u3002\u4f7f\u7528with\u8bed\u53e5\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u8fed\u4ee3\u6570\u636e\u751f\u6210\u5668\uff0c\u5c06\u6bcf\u4e2a\u5757\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b80\u5355\u7684<code>data_generator<\/code>\u751f\u6210\u5668\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u751f\u6210\u56fa\u5b9a\u5927\u5c0f\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u5757\u3002\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u751f\u6210\u5668\u7684\u5b9e\u73b0\uff0c\u4ee5\u8bfb\u53d6\u548c\u751f\u6210\u5b9e\u9645\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u5927\u6587\u4ef6\u548c\u9700\u8981\u5206\u5757\u5199\u5165\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u5185\u5b58\u5360\u7528\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5904\u7406\u6587\u4ef6\u7ed3\u675f\u6807\u5fd7<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5728\u8ffd\u52a0\u6570\u636e\u524d\u68c0\u67e5\u6587\u4ef6\u7ed3\u675f\u6807\u5fd7\uff08EOF\uff09\u3002\u53ef\u4ee5\u4f7f\u7528seek\u51fd\u6570\u79fb\u52a8\u6587\u4ef6\u6307\u9488\u5230\u6587\u4ef6\u672b\u5c3e\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def append_binary_file_with_eof_check(file_path, data):<\/p>\n<p>    with open(file_path, &#39;ab+&#39;) as file:<\/p>\n<p>        file.seek(0, 2)  # \u79fb\u52a8\u6587\u4ef6\u6307\u9488\u5230\u6587\u4ef6\u672b\u5c3e<\/p>\n<p>        file.write(data)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>file_path = &#39;example_eof.bin&#39;<\/p>\n<p>data_to_append = b&#39;\\x09\\x0A\\x0B\\x0C&#39;<\/p>\n<p>append_binary_file_with_eof_check(file_path, data_to_append)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>append_binary_file_with_eof_check<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5728\u8ffd\u52a0\u6570\u636e\u524d\u4f7f\u7528<code>file.seek(0, 2)<\/code>\u5c06\u6587\u4ef6\u6307\u9488\u79fb\u52a8\u5230\u6587\u4ef6\u672b\u5c3e\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>file.write(data)<\/code>\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u8ffd\u52a0\u6570\u636e\u524d\u68c0\u67e5\u6587\u4ef6\u7ed3\u675f\u6807\u5fd7\u7684\u60c5\u51b5\uff0c\u786e\u4fdd\u6570\u636e\u88ab\u6b63\u786e\u8ffd\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u6355\u83b7<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def append_binary_file_with_error_handling(file_path, data):<\/p>\n<p>    try:<\/p>\n<p>        with open(file_path, &#39;ab&#39;) as file:<\/p>\n<p>            file.write(data)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;\u6587\u4ef6 {file_path} \u4e0d\u5b58\u5728\u3002&quot;)<\/p>\n<p>    except PermissionError:<\/p>\n<p>        print(f&quot;\u6ca1\u6709\u6743\u9650\u5199\u5165\u6587\u4ef6 {file_path}\u3002&quot;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;\u5904\u7406\u6587\u4ef6\u65f6\u51fa\u73b0\u9519\u8bef: {e}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>file_path = &#39;example_error.bin&#39;<\/p>\n<p>data_to_append = b&#39;\\x0D\\x0E\\x0F\\x10&#39;<\/p>\n<p>append_binary_file_with_error_handling(file_path, data_to_append)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>append_binary_file_with_error_handling<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f7f\u7528try-except\u8bed\u53e5\u6355\u83b7\u548c\u5904\u7406\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\u3002\u5305\u62ec\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u5e38\u89c1\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528try-except\u8bed\u53e5\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\u4e0d\u4f1a\u5d29\u6e83\uff0c\u5e76\u63d0\u4f9b\u6709\u7528\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6765\u5904\u7406\u66f4\u590d\u6742\u7684\u6587\u4ef6\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>numpy<\/code>\u5e93\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u6587\u4ef6\uff0c\u6216\u8005\u4f7f\u7528<code>pandas<\/code>\u5e93\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u8868\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def append_binary_file_with_numpy(file_path, data):<\/p>\n<p>    with open(file_path, &#39;ab&#39;) as file:<\/p>\n<p>        np_data = np.array(data, dtype=np.uint8)<\/p>\n<p>        np_data.tofile(file)<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>file_path = &#39;example_numpy.bin&#39;<\/p>\n<p>data_to_append = [17, 18, 19, 20]<\/p>\n<p>append_binary_file_with_numpy(file_path, data_to_append)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>numpy<\/code>\u5e93\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u3002\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>append_binary_file_with_numpy<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u6587\u4ef6\u8def\u5f84\u548c\u8981\u8ffd\u52a0\u7684\u6570\u636e\u4f5c\u4e3a\u53c2\u6570\u3002\u4f7f\u7528<code>numpy<\/code>\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u5e76\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u548c\u6587\u4ef6\u683c\u5f0f\uff0c\u63d0\u4f9b\u66f4\u9ad8\u6548\u548c\u4fbf\u6377\u7684\u64cd\u4f5c\u65b9\u6cd5\u3002\u7136\u800c\uff0c\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u53ef\u80fd\u9700\u8981\u989d\u5916\u7684\u4f9d\u8d56\u548c\u5b66\u4e60\u6210\u672c\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u4e2a\u65b9\u9762\u7684\u4ecb\u7ecd\uff0c\u53ef\u4ee5\u770b\u5230\u5728Python\u4e2d\u8ffd\u52a0\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528open\u51fd\u6570\u548c&#39;ab&#39;\u6a21\u5f0f<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u591a\u6570\u7b80\u5355\u7684\u8ffd\u52a0\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u4f7f\u7528with\u8bed\u53e5\u786e\u4fdd\u6587\u4ef6\u5173\u95ed<\/strong>\uff1a\u7b80\u6d01\u4e14\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/li>\n<li><strong>\u5904\u7406\u5927\u6587\u4ef6\u548c\u5206\u5757\u5199\u5165<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u5927\u6587\u4ef6\u548c\u9700\u8981\u5206\u5757\u5199\u5165\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u5904\u7406\u6587\u4ef6\u7ed3\u675f\u6807\u5fd7<\/strong>\uff1a\u5728\u8ffd\u52a0\u6570\u636e\u524d\u68c0\u67e5\u6587\u4ef6\u7ed3\u675f\u6807\u5fd7\u3002<\/li>\n<li><strong>\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u6355\u83b7<\/strong>\uff1a\u786e\u4fdd\u7a0b\u5e8f\u5065\u58ee\u6027\uff0c\u63d0\u4f9b\u6709\u7528\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<li><strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/strong>\uff1a\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u548c\u6587\u4ef6\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<p><p>\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u8ffd\u52a0\u64cd\u4f5c\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5f00\u5e76\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002\u901a\u8fc7\u5c06\u6a21\u5f0f\u8bbe\u7f6e\u4e3a<code>&#39;rb&#39;<\/code>\uff08\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff09\u53ef\u4ee5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;example.bin&#39;, &#39;rb&#39;) as file:\n    data = file.read()\n    print(data)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u6bb5\u4f1a\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a<code>example.bin<\/code>\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u8bfb\u53d6\u5230\u53d8\u91cf<code>data<\/code>\u4e2d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u786e\u4fdd\u4e8c\u8fdb\u5236\u6587\u4ef6\u5199\u5165\u65f6\u4e0d\u4e22\u5931\u6570\u636e\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u5728\u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6\u65f6\u6570\u636e\u4e0d\u4e22\u5931\uff0c\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>&#39;ab&#39;<\/code>\u6a21\u5f0f\uff08\u8ffd\u52a0\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff09\u53ef\u4ee5\u5728\u4e0d\u8986\u76d6\u539f\u6709\u6570\u636e\u7684\u60c5\u51b5\u4e0b\u8ffd\u52a0\u65b0\u6570\u636e\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;example.bin&#39;, &#39;ab&#39;) as file:\n    file.write(b&#39;New binary data&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e86\u6570\u636e\u7684\u5b89\u5168\u6027\u548c\u5b8c\u6574\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u5927\u6587\u4ef6\u7684\u4e8c\u8fdb\u5236\u8ffd\u52a0\uff1f<\/strong><br \/>\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u5efa\u8bae\u9010\u5757\u8bfb\u53d6\u548c\u5199\u5165\u6570\u636e\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u5360\u7528\u8fc7\u9ad8\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u5408\u9002\u7684\u7f13\u51b2\u533a\u5927\u5c0f\u6765\u63a7\u5236\u6bcf\u6b21\u8bfb\u53d6\u7684\u5b57\u8282\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u9010\u5757\u8bfb\u53d6\u6587\u4ef6\u5e76\u8ffd\u52a0\u5230\u53e6\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\uff1a<\/p>\n<pre><code class=\"language-python\">buffer_size = 1024  # \u8bbe\u7f6e\u7f13\u51b2\u533a\u5927\u5c0f\nwith open(&#39;largefile.bin&#39;, &#39;rb&#39;) as source_file, open(&#39;output.bin&#39;, &#39;ab&#39;) as dest_file:\n    while chunk := source_file.read(buffer_size):\n        dest_file.write(chunk)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u6709\u6548\u5730\u7ba1\u7406\u5185\u5b58\uff0c\u4f7f\u5f97\u5904\u7406\u5927\u6587\u4ef6\u65f6\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u8ffd\u52a0\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528open\u51fd\u6570\u3001\u4f7f\u7528with\u8bed\u53e5\u3001\u4ee5\u53ca\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u9009\u62e9\u6b63\u786e\u7684\u6a21\u5f0f\uff08&#038;# [&hellip;]","protected":false},"author":3,"featured_media":1074755,"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\/1074752"}],"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=1074752"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074752\/revisions"}],"predecessor-version":[{"id":1074757,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074752\/revisions\/1074757"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1074755"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1074752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1074752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1074752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}