-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitFile.py
112 lines (100 loc) · 3.84 KB
/
splitFile.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
import sys
#https://stackoverflow.com/questions/291740/how-do-i-split-a-huge-text-file-in-python
import mmap
#https://stackoverflow.com/questions/19684434/best-way-to-check-function-arguments-in-python
import getopt
#https://stackoverflow.com/questions/2682745/how-to-create-a-constant-in-python/19306516#19306516
from named_constants import Constants
class UserMessages(Constants):
MSG1 = "File generated =>"
MSG2 = "Number of files generated: "
MSG3 = "File size in bytes: "
class ErrorMessages(Constants):
#member variables go here
ERR1 = "WARNING: Missing argument=> -i 'c:\inputfile.txt' "
ERR2 = "WARNING: The file does not exist => "
#class methods go here
#do initialization stuff here
""" method example
def car_color(self, colorr="black"):
self.color = cars.paint(colorr)
return True
"""
class Parameters:
def __init__(self, filename):
self.filename = filename
#self.outputpath = outputpath
def main():
version = '0.8'
params = checkarguments()
getfilesize(params)
splitfile(params)
def checkarguments():
if len(sys.argv) < 3:
print(ErrorMessages.ERR1)
sys.exit(-1)
print('ARGV :', sys.argv[1:])
options, remainder = getopt.getopt(sys.argv[1:],
'i:v',
['inputfile=','version=', ])
print ('OPTIONS :', options)
for opt, arg in options:
if opt in ('-i', '--input'):
filename = arg
# elif opt in ('-o', '--outputpath'):
# outputpath = arg
elif opt == '--version':
version = arg
#check if file exists.
if not os.path.isfile(filename):
print(ErrorMessages.ERR2,'"',filename, '"')
sys.exit(-1)
return Parameters(filename)
#file size for Big file management
def getfilesize(parameters):
with open(parameters.filename,"rb") as fr:
fr.seek(0,2) # move to end of the file
size=fr.tell()
print(UserMessages.MSG3, size)
return fr.tell()
def splitfile(parameters):
#splitpoint = parameters.filename.rfind(".")
#orginalfilename = parameters.filename.split(".")
orginalfilename = parameters.filename.rsplit(".",1)
try:
with open(parameters.filename, "r") as fhand:
linecount=0
filecount=0
#se recorre el fichero linea a linea
for line in fhand:
linecount += 1 #contador de lineas
#En las lineas pares se rellena y se cierra el fichero.
if (linecount % 2 == 0): #print "linea PAR"
with open(newfilename,"ab") as fw:
fw.write(bytes(line, 'utf-8'))
fw.close()
#En las lineas impares se crea el fichero con la primera parte.
else: #print "linea IMPAR"
filecount +=1 #contador de ficheros que se generan.
newfilename = orginalfilename[0]+ \
"_{id}.".format(id=str(filecount))+ \
orginalfilename[1]
#fullpath = parameters.outputpath + newfilename
with open(newfilename,"ab") as fw:
fw.seek(0)
fw.truncate()# truncate original if present
fw.write(bytes(line, 'utf-8'))
print( UserMessages.MSG1,"'",newfilename,"'")
print( UserMessages.MSG2, filecount)
fhand.close()
except OSError as err:
print("OS error: {0}".format(err))
sys.exit(-1)
except:
print("Unexpected error:", sys.exc_info()[0])
sys.exit(-1)
#raise
#entry point
if __name__ == "__main__":
main()