{"id":1052996,"date":"2024-12-31T14:30:04","date_gmt":"2024-12-31T06:30:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1052996.html"},"modified":"2024-12-31T14:30:06","modified_gmt":"2024-12-31T06:30:06","slug":"python%e5%a6%82%e4%bd%95%e6%93%8d%e4%bd%9c%e4%ba%8c%e8%bf%9b%e5%88%b6%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1052996.html","title":{"rendered":"python\u5982\u4f55\u64cd\u4f5c\u4e8c\u8fdb\u5236\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/d8342d52-572b-4b5f-9fd9-b8351261235c.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u64cd\u4f5c\u4e8c\u8fdb\u5236\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u64cd\u4f5c\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>read()<\/code>\u548c<code>write()<\/code>\u65b9\u6cd5\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3001\u4f7f\u7528<code>seek()<\/code>\u65b9\u6cd5\u5b9a\u4f4d\u6587\u4ef6\u6307\u9488\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u662f\u6700\u57fa\u672c\u548c\u91cd\u8981\u7684\u64cd\u4f5c\uff0c\u80fd\u591f\u6253\u5f00\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\u5e76\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5177\u4f53\u64cd\u4f5c\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6253\u5f00\u4e8c\u8fdb\u5236\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u53ef\u4ee5\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u3002\u5bf9\u4e8e\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u9700\u8981\u5728\u6a21\u5f0f\u5b57\u7b26\u4e32\u4e2d\u5305\u542b&#39;b&#39;\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u8fdb\u884c\u4e8c\u8fdb\u5236\u8bfb\u53d6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open(filename, &#39;rb&#39;)<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    binary_data = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f(&#39;rb&#39;)\u6253\u5f00\u4e86\u4e00\u4e2a\u540d\u4e3a<code>example.bin<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528<code>read()<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6307\u5b9a\u6570\u91cf\u7684\u5b57\u8282\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u5b57\u8282\u6570\uff0c\u5219\u4f1a\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    binary_data = file.read(1024)  # \u8bfb\u53d6\u524d1024\u5b57\u8282<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u9010\u884c\u8bfb\u53d6<\/h4>\n<\/p>\n<p><p>\u867d\u7136\u4e8c\u8fdb\u5236\u6587\u4ef6\u901a\u5e38\u4e0d\u9002\u5408\u9010\u884c\u8bfb\u53d6\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9010\u884c\u5904\u7406\u53ef\u80fd\u662f\u5fc5\u8981\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7<code>readline()<\/code>\u65b9\u6cd5\u6765\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    line = file.readline()<\/p>\n<p>    while line:<\/p>\n<p>        # \u5904\u7406\u6bcf\u4e00\u884c\u7684\u4e8c\u8fdb\u5236\u6570\u636e<\/p>\n<p>        line = file.readline()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5199\u5165\u7684\u6570\u636e\u5fc5\u987b\u662f\u5b57\u8282\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_to_write = b&#39;\\x00\\x01\\x02\\x03&#39;<\/p>\n<p>with open(&#39;output.bin&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    file.write(data_to_write)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a\u5b57\u8282\u5bf9\u8c61<code>data_to_write<\/code>\u5199\u5165\u5230\u4e00\u4e2a\u540d\u4e3a<code>output.bin<\/code>\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9a\u4f4d\u6587\u4ef6\u6307\u9488<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u79fb\u52a8\u6587\u4ef6\u6307\u9488\u5230\u4e0d\u540c\u7684\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>seek()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    file.seek(10)  # \u5c06\u6587\u4ef6\u6307\u9488\u79fb\u52a8\u5230\u7b2c10\u4e2a\u5b57\u8282<\/p>\n<p>    data = file.read(5)  # \u8bfb\u53d6\u63a5\u4e0b\u6765\u76845\u4e2a\u5b57\u8282<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u83b7\u53d6\u5f53\u524d\u4f4d\u7f6e<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>tell()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u6307\u9488\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    file.seek(10)<\/p>\n<p>    position = file.tell()  # \u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u6307\u9488\u7684\u4f4d\u7f6e<\/p>\n<p>    print(position)  # \u8f93\u51fa\uff1a10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u9010\u5b57\u8282\u6216\u9010\u884c\u8bfb\u53d6\u53ef\u80fd\u66f4\u4e3a\u9ad8\u6548\uff0c\u907f\u514d\u4e00\u6b21\u6027\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5bfc\u81f4\u5185\u5b58\u5360\u7528\u8fc7\u5927\u3002\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u914d\u5408<code>read()<\/code>\u65b9\u6cd5\u5904\u7406\u5927\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;large_file.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    while True:<\/p>\n<p>        chunk = file.read(1024)<\/p>\n<p>        if not chunk:<\/p>\n<p>            break<\/p>\n<p>        # \u5904\u7406\u6bcf\u4e00\u4e2a\u5757\u7684\u6570\u636e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u5e38\u89c1\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u6587\u4ef6\u5934\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u8bb8\u591a\u4e8c\u8fdb\u5236\u6587\u4ef6\u683c\u5f0f\u5728\u6587\u4ef6\u5934\u90e8\u5305\u542b\u4e86\u91cd\u8981\u7684\u5143\u6570\u636e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6\u7684\u524d\u51e0\u4e2a\u5b57\u8282\u6765\u83b7\u53d6\u8fd9\u4e9b\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;image.bmp&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    header = file.read(14)  # \u8bfb\u53d6BMP\u6587\u4ef6\u5934\u7684\u524d14\u4e2a\u5b57\u8282<\/p>\n<p>    print(header)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u89e3\u6790\u7ed3\u6784\u5316\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4e8c\u8fdb\u5236\u6587\u4ef6\u5e38\u5e38\u5305\u542b\u7ed3\u6784\u5316\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>struct<\/code>\u6a21\u5757\u6765\u89e3\u6790\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import struct<\/p>\n<p>with open(&#39;data.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    data = file.read(8)  # \u5047\u8bbe\u6bcf\u4e2a\u6570\u636e\u5757\u662f8\u5b57\u8282<\/p>\n<p>    unpacked_data = struct.unpack(&#39;II&#39;, data)  # \u89e3\u6790\u4e3a\u4e24\u4e2a\u65e0\u7b26\u53f7\u6574\u6570<\/p>\n<p>    print(unpacked_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u6587\u4ef6\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\u5e94\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u589e\u5f3a\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;example.bin&#39;, &#39;rb&#39;) as file:<\/p>\n<p>        binary_data = file.read()<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&quot;\u6587\u4ef6\u672a\u627e\u5230&quot;)<\/p>\n<p>except IOError:<\/p>\n<p>    print(&quot;\u6587\u4ef6\u8bfb\u5199\u9519\u8bef&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u64cd\u4f5c\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002\u901a\u8fc7\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u7ed3\u5408<code>read()<\/code>\u3001<code>write()<\/code>\u3001<code>seek()<\/code>\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u9ad8\u6548\u5730\u8bfb\u5199\u548c\u64cd\u4f5c\u4e8c\u8fdb\u5236\u6587\u4ef6\u6570\u636e\u3002\u6b64\u5916\uff0c\u7ed3\u5408\u5f02\u5e38\u5904\u7406\u548c\u6570\u636e\u89e3\u6790\u6a21\u5757\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u589e\u5f3a\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u529f\u80fd\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u7b56\u7565\uff0c\u80fd\u591f\u6709\u6548\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\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\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5177\u4f53\u65b9\u6cd5\u662f\u5c06\u6a21\u5f0f\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>&#39;rb&#39;<\/code>\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>with open(&#39;file.bin&#39;, &#39;rb&#39;) as file:<\/code>\u53ef\u4ee5\u5b89\u5168\u5730\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5176\u4e2d\u7684\u6570\u636e\u3002\u8bfb\u53d6\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528<code>file.read()<\/code>\u3001<code>file.readline()<\/code>\u6216<code>file.readlines()<\/code>\u65b9\u6cd5\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u9700\u8981\u8bfb\u53d6\u7684\u6570\u636e\u91cf\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6\uff1f<\/strong><br \/>\u4e3a\u4e86\u5199\u5165\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u4ee5\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u6a21\u5f0f\u53c2\u6570\u5e94\u8bbe\u7f6e\u4e3a<code>&#39;wb&#39;<\/code>\u3002\u793a\u4f8b\u4ee3\u7801\u4e3a<code>with open(&#39;output.bin&#39;, &#39;wb&#39;) as file:<\/code>\u3002\u901a\u8fc7<code>file.write(data)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u5728\u5199\u5165\u6570\u636e\u4e4b\u524d\uff0c\u786e\u4fdd\u6570\u636e\u662f\u4ee5\u5b57\u8282\u5f62\u5f0f\u8868\u793a\uff0c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7<code>bytes()<\/code>\u51fd\u6570\u6216<code>bytearray()<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u4e2d\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u6570\u636e\uff1f<\/strong><br \/>\u5904\u7406\u4e8c\u8fdb\u5236\u6587\u4ef6\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>struct<\/code>\u6a21\u5757\u8fdb\u884c\u6570\u636e\u7684\u6253\u5305\u548c\u89e3\u5305\u3002\u8fd9\u5141\u8bb8\u60a8\u5c06Python\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u8868\u793a\u5f62\u5f0f\uff0c\u6216\u8005\u4ece\u4e8c\u8fdb\u5236\u6570\u636e\u4e2d\u8bfb\u53d6\u8fd9\u4e9b\u7c7b\u578b\u3002\u901a\u8fc7\u4f7f\u7528<code>struct.pack()<\/code>\u548c<code>struct.unpack()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7cbe\u786e\u63a7\u5236\u6570\u636e\u7684\u683c\u5f0f\u548c\u5b57\u8282\u987a\u5e8f\uff0c\u786e\u4fdd\u5728\u8bfb\u53d6\u548c\u5199\u5165\u65f6\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u64cd\u4f5c\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528open()\u51fd\u6570\u3001\u4f7f\u7528read()\u548cwrite()\u65b9\u6cd5\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1053005,"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\/1052996"}],"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=1052996"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1052996\/revisions"}],"predecessor-version":[{"id":1053008,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1052996\/revisions\/1053008"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1053005"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1052996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1052996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1052996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}