-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextsplitter.py
executable file
·93 lines (82 loc) · 2.75 KB
/
textsplitter.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
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
from pathlib import Path
import pathlib
import tempfile
def print_mult(x, MAX):
cont = 0
arr = set()
for i in range(2, x+1):
if(x % i == 0):
cont += 1
arr.add(i)
if(cont > MAX):
break
print(arr)
return
def partitions(reading_stream, select, ftmp, totlines):
# looking how many lines are
for line in reading_stream:
if(select == 'y' and not(line.isspace())):
ftmp.write(line)
totlines += 1
elif(select == 'n'):
totlines += 1
if(select == 'y'):
ftmp.seek(0, 0)
MAX = 15 # maximum number of suggested partitions
print("Found " + str(totlines) + " lines.")
print("\nHere you are some suggested legit partition size\\s to use: ")
print_mult(totlines, MAX)
part = int(input("\nIn how many partitions do you want to split the file? "))
if(part > totlines != 0):
print("Error: partitions greater than totlines!")
quit()
if(totlines % part != 0):
print("Error: partitions number has to be a multiple of your total file lines!")
quit()
return part, totlines
def inputf():
print("Remember to add file extension (.log, .txt..) ")
src_filename = input("file name: ")
reading_stream = open(src_filename, "r")
select = input(
"Would you like to exclude whitespaces\\whitelines?\nSuggested 'y'\n[y\\n] ")
if(not(select == 'y' or select == 'n')):
print("Error")
quit()
path = str(pathlib.Path().parent.absolute()) + "/" + src_filename
noext_filename = Path(path).resolve().stem
ext = str(pathlib.Path(src_filename).suffix)
if(select == 'y'):
ftmp = tempfile.TemporaryFile(mode='w+')
return select, reading_stream, noext_filename, ext, ftmp
else:
return select, reading_stream, noext_filename, ext, False
def main():
select, reading_stream, noext_filename, ext, ftmp = inputf()
part, totlines = partitions(reading_stream, select, ftmp, 0)
cont = 1
reading_stream.seek(0, 0)
if(select == 'y'):
ftmp.seek(0, 0)
linestoprint = int(totlines / part)
for cont in range(1, part+1):
f = open(noext_filename + "-part-" + str(cont) + ext, "w")
while (k<linestoprint):
progress = float((cont) / part) * 100
if(select == 'n'):
line = str(reading_stream.readline())
f.write(line)
else:
line = str(ftmp.readline())
f.write(line)
print("Progress: " + "%2.f" % progress + "%")
k+=1
f.close()
k = 0
reading_stream.close()
if(select == 'y'):
ftmp.close()
return
if(__name__ == "__main__"):
main()