{"id":342,"date":"2014-07-03T02:49:39","date_gmt":"2014-07-03T02:49:39","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=342"},"modified":"2014-07-03T02:49:39","modified_gmt":"2014-07-03T02:49:39","slug":"python%e7%89%88%e6%89%b9%e9%87%8f%e6%a3%80%e6%b5%8b%e5%b9%b6%e5%88%a0%e9%99%a4bom%e7%9a%84%e8%84%9a%e6%9c%acbak","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/342.html","title":{"rendered":"Python\u7248\u6279\u91cf\u68c0\u6d4b\u5e76\u5220\u9664BOM\u7684\u811a\u672c[bak]"},"content":{"rendered":"<p>\u627e\u5230\u4e86\u4e2aPython\u7248\u7684BOM\u6587\u4ef6\u5934\u6279\u91cf\u68c0\u6d4b\u53ca\u5220\u9664\u7684\u811a\u672c\uff1a<\/p>\n<pre class=\"lang:python decode:true \">#!\/usr\/bin\/env python\n#-*- coding: utf-8 -*-\n\nimport os\n\ndef delBOM():\n\tfile_count = 0\n\tbom_files = []\n\nfor dirpath, dirnames, filenames in os.walk('.'):\n\tif(len(filenames)):\n\t\tfor filename in filenames:\n\t\t\tfile_count += 1\n\t\t\tfile = open(dirpath + \"\/\" + filename, 'r+')\n\t\t\tfile_contents = file.read()\n\n\t\t\tif(len(file_contents) &gt; 3):\n\t\t\t\tif(ord(file_contents[0]) == 239 and ord(file_contents[1]) == 187 and ord(file_contents[2]) == 191):\n\t\t\t\t\tbom_files.append(dirpath + \"\/\" + filename)\n\t\t\t\t\tfile.seek(0)\n\t\t\t\t\tfile.write(file_contents[3:])\n\t\t\t\t\tprint bom_files[-1], \"BOM found. Deleted.\"\n\t\t\tfile.close()\n\nprint file_count, \"file(s) found.\", len(bom_files), \"file(s) have a bom. Deleted.\"\n\nif __name__ == \"__main__\":\n\tdelBOM()<\/pre>\n<p>Python\u7248\u672c\u7684\u76f8\u6bd4PHP\u7248\u7684\u8981\u7b80\u6d01\u591a\u4e86{\u771f\u4e0d\u6127\u662f\uff1a\u4eba\u751f\u82e6\u77ed\uff0c\u6211\u7528Python}<br \/>\n\u4e3b\u8981\u4f53\u73b0\u5728\uff1a<br \/>\n\u4f7f\u7528os.walk()\u5b9e\u73b0\u9012\u5f52\u904d\u5386\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\uff1b<br \/>\n\u5217\u8868\u64cd\u4f5c\u7684\u5feb\u901f\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u627e\u5230\u4e86\u4e2aPython\u7248\u7684BOM\u6587\u4ef6\u5934\u6279\u91cf\u68c0\u6d4b\u53ca\u5220\u9664\u7684\u811a\u672c\uff1a #!\/usr\/bin\/env python #-* [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,12],"tags":[161,162,8],"class_list":["post-342","post","type-post","status-publish","format-standard","hentry","category-programing","category-tools","tag-list","tag-os-walk","tag-python"],"views":2276,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=342"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/342\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}