import sys
import os
class file_openate:
def __init__(self):
self.chunksize = int(1024*1024*5)
self.fromfile = '/root/mysql-5.7.25-el7-x86_64.tar.gz'
self.todir = '/tmp/split-file/'
self.partnum = 0
def check(self):
if not os.path.exists(self.todir):
os.makedirs(self.todir)
def split(self):
if len(sys.argv) == 2:
self.fromfile = sys.argv[1]
if not os.path.isfile(self.fromfile):
print('文件不存在')
return
fread = open(self.fromfile, 'rb')
while True:
chunk = fread.read(self.chunksize)
if not chunk:
break
self.partnum += 1
filename = ('%s/part-%s') % (self.todir, str(self.partnum).rjust(3, '0'))
fileobj = open(filename, 'wb')
fileobj.write(chunk)
fileobj.close()
def merge(self):
outfile = open(self.fromfile, 'wb')
files = os.listdir(self.todir)
files.sort()
for file in files:
print(file)
filepath = os.path.join(self.todir, file)
infile = open(filepath, 'rb')
data = infile.read()
outfile.write(data)
infile.close()
outfile.close()
if __name__ == "__main__":
fileop = file_openate()
fileop.check()
# fileop.split()
fileop.merge()