Skip to content

Instantly share code, notes, and snippets.

@ryonsherman
Last active August 29, 2015 14:21
Show Gist options
  • Save ryonsherman/9432f43786b09d1f1a9e to your computer and use it in GitHub Desktop.
Save ryonsherman/9432f43786b09d1f1a9e to your computer and use it in GitHub Desktop.
Determine whether a file is binary or text
#!/usr/bin/env python2
def fileIsBinary(path):
text = bytearray([7, 8, 9, 10, 12, 13, 27]) + bytearray(range(0x20, 0x100))
return bool(open(path, 'rb').read(1024).translate(None, text))
@ryonsherman
Copy link
Author

Example

>>> fileIsBinary('/usr/bin/ls')
True
>>> fileIsBinary('/etc/issue')
False

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment