Skip to content

Instantly share code, notes, and snippets.

@allenanie
Created April 19, 2014 16:49
Show Gist options
  • Save allenanie/11090051 to your computer and use it in GitHub Desktop.
Save allenanie/11090051 to your computer and use it in GitHub Desktop.
Python File Encoding Detect with CLI Tool
import cmd
import string, sys
from chardet.universaldetector import UniversalDetector
class CLI(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.prompt = '$'
self.path = ''
self.intro = '''Encoding Detection Usage Guide:
path 'file name' - set up file path (could be absolute or relative path)
scan - start scanning
quit - exit the program
'''
def help_quit(self):
print("Exit the program.")
def do_quit(self, line):
sys.exit()
def help_path(self):
print("set up file path (could be absolute or relative path)")
def do_path(self, pathName):
if pathName == "": pathName = raw_input("Type the file path: ")
if pathName != "":
self.path = pathName
def help_scan(self):
print("Must set up path first before scanning")
def do_scan(self, anything):
if self.path == "":
print("set up path first!")
else:
detector = UniversalDetector()
with open('/Users/Aimingnie/Desktop/webapp/0001193125-12-511683.txt', 'r') as f:
for line in f.readlines():
detector.feed(line)
if detector.done: break
detector.close()
print detector.result
if __name__ == '__main__':
cdc = CLI()
cdc.cmdloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment