I need to write a script that will check a file and find out what type of file it is. The files I am downloading none of them have extensions and some are binary others txt and html files. I only want to work with the binary files.

If anyone could point me in the needed direction it would be great.

thank you in advance.

Dani AI

Generated

For : was right to hint at "magic" — relying on MIME detection is the most reliable route. Two practical approaches are shown below: prefer libmagic (or the POSIX file command) for accurate MIME-based decisions, and fall back to a fast heuristic that only reads the first few KB when libmagic is unavailable.

# libmagic (python-magic) approach
try:
    import magic
except ImportError:
    magic = None

def is_binary_magic(path):
    if magic is None:
        raise RuntimeError('python-magic not available')
    m = magic.Magic(mime=True)
    mime = m.from_file(path)
    text_like = ('text/', 'application/json', 'application/javascript',
                 'application/xml', 'application/xhtml+xml')
    if mime.startswith('text/') or any(mime == t or mime.startswith(t) for t in text_like):
        return False
    return True
# fallback: call the 'file' utility (POSIX)
import subprocess

def is_binary_filecmd(path):
    p = subprocess.run(['file', '--mime-type', '-b', path],
                       stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
    mime = (p.stdout or '').strip()
    return not mime.startswith('text/')
# cheap heuristic fallback: NUL byte or high non-printable ratio
def is_binary_heuristic(path, sample_size=4096, nontext_threshold=0.30):
    with open(path, 'rb') as f:
        chunk = f.read(sample_size)
    if not chunk:
        return False
    if b'\x00' in chunk:
        return True
    low = chunk.lower()
    if b'<html' in low or b'<!doctype' in low:
        return False
    printable = set(range(0x20, 0x7f)) | {9, 10, 13}
    nontext = sum(1 for b in chunk if b not in printable)
    return (nontext / len(chunk)) > nontext_threshold

Combine these: try libmagic, then file, then the heuristic. Read only a small sample to keep performance acceptable. Note that heuristics can misclassify some UTF-8 text or packed binary that happens to look textual; log edge cases for manual review if accuracy matters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.