-
Notifications
You must be signed in to change notification settings - Fork 8
/
convcat.py
37 lines (33 loc) · 885 Bytes
/
convcat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import shutil
try:
from cchardet import UniversalDetector
except ImportError:
from chardet.universaldetector import UniversalDetector
detector = UniversalDetector()
arg = sys.argv[1:]
if arg:
fns = list(filter(os.path.isfile, arg))
else:
fns = ['-']
for fn in fns:
if fn == '-':
stream = sys.stdin.buffer
else:
stream = open(fn, 'rb')
detector.reset()
cache = []
for line in stream:
detector.feed(line)
cache.append(line)
if detector.done:
break
detector.close()
for line in cache:
sys.stdout.write(line.decode(detector.result['encoding'] or 'utf-8', errors='replace'))
for line in stream:
sys.stdout.write(line.decode(detector.result['encoding'] or 'utf-8', errors='replace'))
stream.close()