-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilemerger.py
More file actions
executable file
·97 lines (79 loc) · 2.8 KB
/
filemerger.py
File metadata and controls
executable file
·97 lines (79 loc) · 2.8 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
import chardet
import io
import os
import shutil
def is_plain_text_file(path):
"""Check if the input is a plain text file.
Tested with https://github.com/stain/encoding-test-files
and https://www.kermitproject.org/utf8.html
"""
with open(path, "rb") as file:
data = file.read()
result = chardet.detect(data)
if (
result["encoding"] is None
or result["encoding"] == "UTF-16"
or result["encoding"] == "UTF-32"
):
return False
else:
return True
def merger(input_dir, separator, output_dir, output_file):
"""Merging all plain text files.
A line containing a given string is used as a separator.
"""
file_list = [
os.path.join(input_dir, f)
for f in os.listdir(input_dir)
if os.path.isfile(os.path.join(input_dir, f))
and is_plain_text_file(os.path.join(input_dir, f))
and f[0] != "."
]
with open(os.path.join(output_dir, output_file), "wb") as merged_file:
for i, f in enumerate(file_list):
with open(f, "rb") as elem:
shutil.copyfileobj(elem, merged_file)
if i < len(file_list) - 1:
encoded_separator = ("\n" + separator + "\n").encode()
elem = io.BytesIO(encoded_separator)
shutil.copyfileobj(elem, merged_file)
def main():
flag = True
while flag:
text_dir = input(
"\nPlease, insert the full path of the directory containing "
+ "the plain text files to be merged. \n"
)
if os.path.isdir(text_dir):
flag = False
else:
print(" Directory is invalid or non-existent.")
separating = input(
"\nPlease, insert the string to write in the line separating the "
+ "different files (press Enter for a blank line). \n"
)
flag = True
while flag:
saving_dir = input(
"\nPlease, insert the full path of the directory where the "
+ "result should be saved. \n"
)
if os.path.isdir(saving_dir):
flag = False
else:
print(" Directory is invalid or non-existent.")
flag = True
while flag:
saving_file = input(
"\nPlease, insert the filename (e.g. result.xyz) where the "
+ "result should be saved. \n"
)
check_file = os.path.join(saving_dir, saving_file)
if os.path.isfile(check_file):
print(" The file already exists: please choose another name.")
else:
flag = False
merger(text_dir, separating, saving_dir, saving_file)
if __name__ == "__main__":
main()