{"id":49404,"date":"2024-12-02T10:43:27","date_gmt":"2024-12-02T02:43:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49404\/"},"modified":"2024-12-02T10:43:27","modified_gmt":"2024-12-02T02:43:27","slug":"python%e4%b8%adhash_file%e5%87%bd%e6%95%b0%e5%a4%84%e7%90%86%e4%b8%ad%e6%96%87%e6%96%87%e4%bb%b6%e5%90%8d%e6%8a%a5%e9%94%99%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49404\/","title":{"rendered":"Python\u4e2dhash_file()\u51fd\u6570\u5904\u7406\u4e2d\u6587\u6587\u4ef6\u540d\u62a5\u9519\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Python\u4e2dhash_file()\u51fd\u6570\u5904\u7406\u4e2d\u6587\u6587\u4ef6\u540d\u62a5\u9519\u5982\u4f55\u89e3\u51b3\uff1f<\/h1>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPython\u4e2dhash_file()\u51fd\u6570\u5904\u7406\u4e2d\u6587\u6587\u4ef6\u540d\u62a5\u9519\u5982\u4f55\u89e3\u51b3\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241201\/1733026180674be184f1eb4.jpg\" class=\"aligncenter\" title=\"Python\u4e2dhash_file()\u51fd\u6570\u5904\u7406\u4e2d\u6587\u6587\u4ef6\u540d\u62a5\u9519\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" alt=\"Python\u4e2dhash_file()\u51fd\u6570\u5904\u7406\u4e2d\u6587\u6587\u4ef6\u540d\u62a5\u9519\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u7cfb\u7edf\u53c2\u6570\u8bbe\u7f6e\u4e2d hash_file() \u62a5\u9519\uff1a\u65e0\u6cd5\u6253\u5f00\u6d41\uff1a\u65e0\u6548\u7684\u53c2\u6570<\/strong><\/p>\n<p>\u5728\u8bbe\u7f6e\u7cfb\u7edf\u53c2\u6570\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f7f\u7528 hash_file() \u51fd\u6570\u65f6\u9047\u5230\u4e86\u9519\u8bef\uff0c\u63d0\u793a\u201c\u65e0\u6cd5\u6253\u5f00\u6d41\uff1a\u65e0\u6548\u7684\u53c2\u6570\u201d\u3002\u8fd9\u4e2a\u95ee\u9898\u901a\u5e38\u53d1\u751f\u5728\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\u65f6\u3002<\/p>\n<p><strong>\u95ee\u9898\u539f\u56e0<\/strong><\/p>\n<p>hash_file() \u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>\u6587\u4ef6\u540d\uff08string\uff09<\/li>\n<li>\u54c8\u5e0c\u7b97\u6cd5\uff08string\uff09<\/li>\n<\/ul>\n<p>\u4e2d\u6587\u6587\u4ef6\u540d\u5e76\u4e0d\u662f\u4e00\u4e2a\u6709\u6548\u7684 string \u7c7b\u578b\uff0c\u56e0\u4e3a python \u89e3\u91ca\u5668\u65e0\u6cd5\u8bc6\u522b\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u8981\u628a\u4e2d\u6587\u6587\u4ef6\u540d\u8f6c\u5316\u4e3a\u62fc\u97f3\u6216\u5176\u4ed6\u80fd\u591f\u88ab python \u89e3\u91ca\u5668\u8bc6\u522b\u7684\u5b57\u7b26\u4e32\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>import pinyin\n\ndef convert_to_pinyin(filename):\n    result = []\n    for char in filename:\n        result.extend(pinyin.get(char, char))\n    return ''.join(result)\n\nfilename = convert_to_pinyin(\"up\/1437616281\u5982\u4f55\u6839\u636e\u4f01\u4e1a\u60c5\u51b5\u8bbe\u7f6e\u7cfb\u7edf\u53c2\u6570\uff1f.doc\")\n\nwith open(filename, 'rb') as f:\n    hash = hashlib.sha256(f.read()).hexdigest()<\/pre>\n<p>\u8fd9\u6837\uff0chash_file() \u51fd\u6570\u5c31\u53ef\u4ee5\u6b63\u5e38\u6253\u5f00\u6587\u4ef6\u5e76\u8ba1\u7b97\u54c8\u5e0c\u503c\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPython\u4e2dhash_file()\u51fd\u6570\u5904\u7406\u4e2d\u6587\u6587\u4ef6\u540d\u62a5\u9519\u5982\u4f55\u89e3\u51b3\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2dhash_file()\u51fd\u6570\u5904\u7406\u4e2d\u6587\u6587\u4ef6\u540d\u62a5\u9519\u5982\u4f55\u89e3\u51b3\uff1f \u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u300aPython\u4e2dhash_file()\u51fd\u6570\u5904\u7406\u4e2d\u6587\u6587\u4ef6\u540d\u62a5\u9519\u5982\u4f55\u89e3\u51b3\uff1f\u300b\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 \u7cfb\u7edf\u53c2\u6570\u8bbe\u7f6e\u4e2d hash_file() \u62a5\u9519\uff1a\u65e0\u6cd5\u6253\u5f00\u6d41\uff1a\u65e0\u6548\u7684\u53c2\u6570 \u5728\u8bbe\u7f6e\u7cfb\u7edf\u53c2\u6570\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f7f\u7528 hash_file() \u51fd\u6570\u65f6\u9047\u5230\u4e86\u9519\u8bef\uff0c\u63d0\u793a\u201c\u65e0\u6cd5\u6253\u5f00\u6d41\uff1a\u65e0\u6548\u7684\u53c2\u6570\u201d\u3002\u8fd9\u4e2a\u95ee\u9898\u901a\u5e38\u53d1\u751f\u5728\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\u65f6\u3002 \u95ee\u9898\u539f\u56e0 hash_file() \u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a \u6587\u4ef6\u540d\uff08string\uff09 \u54c8\u5e0c\u7b97\u6cd5\uff08string\uff09 \u4e2d\u6587\u6587\u4ef6\u540d\u5e76\u4e0d\u662f\u4e00\u4e2a\u6709\u6548\u7684 string \u7c7b\u578b\uff0c\u56e0\u4e3a python \u89e3\u91ca\u5668\u65e0\u6cd5\u8bc6\u522b\u4e2d\u6587\u5b57\u7b26\u3002 \u89e3\u51b3\u65b9\u6848 \u8981\u628a\u4e2d\u6587\u6587\u4ef6\u540d\u8f6c\u5316\u4e3a\u62fc\u97f3\u6216\u5176\u4ed6\u80fd\u591f\u88ab python \u89e3\u91ca\u5668\u8bc6\u522b\u7684\u5b57\u7b26\u4e32\u3002\u5982\u4e0b\u6240\u793a\uff1a import pinyin def convert_to_pinyin(filename): result = [] for char in filename: result.extend(pinyin.get(char, char)) return &#8221;.join(result) filename = convert_to_pinyin(&#8220;up\/1437616281\u5982\u4f55\u6839\u636e\u4f01\u4e1a\u60c5\u51b5\u8bbe\u7f6e\u7cfb\u7edf\u53c2\u6570\uff1f.doc&#8221;) with open(filename, &#8216;rb&#8217;) as f: hash = hashlib.sha256(f.read()).hexdigest() \u8fd9\u6837\uff0chash_file() \u51fd\u6570\u5c31\u53ef\u4ee5\u6b63\u5e38\u6253\u5f00\u6587\u4ef6\u5e76\u8ba1\u7b97\u54c8\u5e0c\u503c\u3002 \u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPython\u4e2dhash_file()\u51fd\u6570\u5904\u7406\u4e2d\u6587\u6587\u4ef6\u540d\u62a5\u9519\u5982\u4f55\u89e3\u51b3\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-49404","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=49404"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49404\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}