3 Script which takes one or more file paths and reports on their detected
8 % chardetect somefile someotherfile
9 somefile: windows-1252 with confidence 0.5
10 someotherfile: ascii with confidence 1.0
12 If no paths are provided, it takes its input from stdin.
16 from sys import argv, stdin
18 from chardet.universaldetector import UniversalDetector
21 def description_of(file, name='stdin'):
22 """Return a string describing the probable encoding of a file."""
23 u = UniversalDetector()
28 if result['encoding']:
29 return '%s: %s with confidence %s' % (name,
33 return '%s: no result' % name
38 print(description_of(stdin))
41 with open(path, 'rb') as f:
42 print(description_of(f, path))
45 if __name__ == '__main__':