-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeTxt.py
81 lines (64 loc) · 1.75 KB
/
MergeTxt.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import cchardet
import re
# coding = gbk
def get_file_list(p):
p = str(p)
if p == "":
return []
p = p.replace("/", "\\")
if p[-1] != "\\":
p = p + "\\"
listdir = os.listdir(p)
sub_files = []
for f in listdir:
path = p + f
if not os.path.isfile(path):
continue
if not re.match("([0-9]+)\\.txt", f):
continue
name, ext = os.path.splitext(path)
if ext != ".txt":
continue
sub_files.append(path)
return sub_files
def get_encoding(file):
with open(file, 'rb') as my_f:
return cchardet.detect(my_f.read())['encoding']
# has risks of incompatible characters
def append_contents(o_file, f_path, encoding):
try:
in_file = open(f_path, "r", encoding=encoding)
all_lines = in_file.readlines()
o_file.writelines(all_lines)
in_file.close()
return True
except (UnicodeEncodeError, UnicodeDecodeError) as e:
print(e)
return False
# copy file contents for sure
def append_binaries(f_path):
try:
o_file = open(all_file, 'ab')
in_file = open(f_path, "rb")
contents = in_file.read()
o_file.write(contents)
o_file.close()
return True
except (FileExistsError, FileNotFoundError) as e:
print(e)
return False
all_file = "all.txt"
txt_files = get_file_list(".")
print(txt_files)
os.remove(all_file)
for f in txt_files:
out_file = open(all_file, 'a')
enc_type = get_encoding(f)
print("append {0} encoding: {1} ".format(f, enc_type))
out_file.write('\r\n')
ret = append_contents(out_file, f, enc_type)
out_file.close()
if not ret:
append_binaries(f)
print("Merge Completed!")