from subprocess import Popen, PIPE
filetype = Popen('/usr/bin/file -b --mime -', shell=True, stdout=PIPE,
    stdin=PIPE).communicate(file.read(1024))[0].strip()
Check whether a PDF-File is valid (Python)