谢谢大家,我找到了适合我问题的解决方案。我在http://pre.activestate.com/recipes/173220/上找到了此代码,并做了一些修改以适合我。
它工作正常。
from __future__ import divisionimport stringdef istext(filename): s=open(filename).read(512) text_characters = "".join(map(chr, range(32, 127)) + list("nrtb")) _null_trans = string.maketrans("", "") if not s: # Empty files are considered text return True if " " in s: # Files with null bytes are likely binary return False # Get the non-text characters (maps a character to itself then # use the 'remove' option to get rid of the text characters.) t = s.translate(_null_trans, text_characters) # If more than 30% non-text characters, then # this is considered a binary file if float(len(t))/float(len(s)) > 0.30: return False return True

![如何使用Python识别二进制文件和文本文件?[重复] 如何使用Python识别二进制文件和文本文件?[重复]](http://www.mshxw.com/aiimages/31/660154.png)
