python文件打开方式二进制或文本_如何使用Python识别的二进制和文本文件?

I need identify which file is binary and which is a text in a directory.

I tried use mimetypes but it isnt a good idea in my case because it cant identify all files mimes, and I have strangers ones here... I just need know, binary or text. Simple ? But I couldn´t find a solution...

Thanks

解决方案

Thanks everybody, I found a solution that suited my problem. I found this code at http://code.activestate.com/recipes/173220/ and I changed just a little piece to suit me.

It works fine.

from __future__ import division

import string

def istext(filename):

s=open(filename).read(512)

text_characters = "".join(map(chr, range(32, 127)) + list("\n\r\t\b"))

_null_trans = string.maketrans("", "")

if not s:

# Empty files are considered text

return True

if "\0" 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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值