-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv-splitter.py
73 lines (60 loc) · 1.93 KB
/
csv-splitter.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
#!/usr/bin/env python3
import os
from posixpath import splitext
MAX_LINES=1048575
# colours for text output
ERR_COL='\033[91m'
WAR_COL = '\033[93m'
END_COL='\033[0m'
def split(filename):
# opens file
print("opening file {}".format(filename))
csvfile = open("./input/{}".format(filename), 'r').readlines()
print("{} file opened".format(filename))
filecount = 1
# split file
for i in range(len(csvfile)):
if i % MAX_LINES == 0:
open("./output/{}_{}.csv".format(splitext(filename)[0], filecount), 'w+').writelines(csvfile[i:i+MAX_LINES])
filecount += 1
print("finished splitting: {}".format(filename))
def main():
# get list of all files in input dir
path = "./input"
csv_list = os.listdir(path)
if len(csv_list) == 0:
print("No files in input folder. \nExitting")
exit()
# read user input
while True:
try:
print("The following files will be split:")
print(csv_list)
cont_input = input("Continue? (y/n):")
except KeyboardInterrupt:
print("\nExiting")
exit()
else:
if cont_input == "y" or cont_input == "Y":
break
elif cont_input == "n" or cont_input == "N":
print("\nExiting")
exit()
else:
print("Input not recognized. Try again")
continue
# splits all files in csv_list
error_files = []
for file in csv_list:
if file.endswith('.csv'):
split(file)
else:
print("{}{} is not a csv file{}".format(WAR_COL, file, END_COL))
error_files.append(file)
# prints any files which were not split
if len(error_files) != 0:
print("\n{}The following files did not properly split:".format(ERR_COL))
print(csv_list)
print(END_COL)
if __name__ == "__main__":
main()