How totell a binary file from a nonbinary file.
Phil Austin
phil at geog.ubc.ca
Sun Oct 29 12:58:20 EST 2000
olczyk at interaccess.com (Thaddeus L. Olczyk) writes:
> There are some changes that I have to make to s directory of files (
> in python of course ). Is there some simple way of telling whether
> a file is binary or not (from python of course )?
Something like this might help:
#
# Detects if files contain text or binary data
# Jeff Epler jepler at inetnebr.com 20 April 97
#
import string
import os
def mask(charlist):
"""Construct a mask suitable for string.translate,
which marks letters in charlist as "t" and ones not as "b" """
mask=""
for i in range(256):
if chr(i) in charlist: mask=mask+"t"
else: mask=mask+"b"
return mask
ascii7bit=string.joinfields(map(chr, range(32,127)), "")+"\r\n\t\b"
ascii7bit_mask=mask(ascii7bit)
def istext(file, check=1024, mask=ascii7bit_mask):
"""Returns true if the first check characters in file
are within mask, false otherwise"""
try:
s=file.read(check)
s=string.translate(s, mask)
if string.find(s, "b") != -1: return 0
return 1
except (AttributeError, NameError): # Other exceptions?
return istext(open(file, "r"))
def main(argv):
for filename in argv:
print filename+":",
if os.path.isfile(filename):
if istext(filename): print "text"
else: print "binary"
elif os.path.isdir(filename):
print "directory"
else: print "not file or directory"
if __name__=='__main__':
import sys
main(sys.argv[1:])
More information about the Python-list
mailing list