Welcome, guest | Sign In | My Account | Store | Cart
import os
import sys

def main():
    try:
        table = [0] * 256
        data = open(sys.argv[1], 'rb')
        buff = data.read(2 ** 20)
        while buff:
            for c in buff:
                table[ord(c)] += 1
            buff = data.read(2 ** 20)
        data.close()
        sys.stdout.write(
            '\n'.join('%02X = %d' % (i, c) for i, c in enumerate(table) if c))
    except:
        sys.stdout.write('Usage: %s <filename>' % os.path.basename(sys.argv[0]))

if __name__ == '__main__':
    main()

Diff to Previous Revision

--- revision 1 2005-11-21 08:56:47
+++ revision 2 2012-07-04 01:36:51
@@ -1,13 +1,20 @@
-from sys import argv
-try:
-    dictionary = dict()
-    for byte in file(argv[1]).read():
-        try:
-            dictionary[byte] += 1
-        except:
-            dictionary[byte] = 1
-    for key in dictionary:
-        print repr(key)[1:-1], '=', dictionary[key]
-except:
-    from os.path import basename
-    print basename(argv[0]), '<filename>'
+import os
+import sys
+
+def main():
+    try:
+        table = [0] * 256
+        data = open(sys.argv[1], 'rb')
+        buff = data.read(2 ** 20)
+        while buff:
+            for c in buff:
+                table[ord(c)] += 1
+            buff = data.read(2 ** 20)
+        data.close()
+        sys.stdout.write(
+            '\n'.join('%02X = %d' % (i, c) for i, c in enumerate(table) if c))
+    except:
+        sys.stdout.write('Usage: %s <filename>' % os.path.basename(sys.argv[0]))
+
+if __name__ == '__main__':
+    main()

History