1.归并
import os
# 1.获取需要整合的文件目录下的
filepath = "G:\我的python\尹成\python基础\day13\详细分类\详细地区"
mylist = os.listdir(filepath)
# 2.创建需要整合的文件
bigfile = open("big.txt","wb") #写入
# 3.开始合并
for data in mylist: #循环每一个文件名
datapath = filepath + "\" + data #路径
datafile = open(datapath,"rb") #读取
for line in datafile: #每一行地区之后写入
bigfile.write(line)
datafile.close()
bigfile.close()
2.分割
import os
# 创建文件夹
# os.mkdir("切割文件")
# 1.创建被切割的文件保存的文件夹
num = 10
splitfilelist = [] #文件集合
for i in range(num):
splitpath = "切割文件\data"+ str(i+1) + ".txt"
splitfile = open(splitpath,"wb") # 写入
splitfilelist.append(splitfile)
# 2.处理要切割的文件
bigfile = open("big.txt","rb")
i = 0
for line in bigfile: #归并
# 分10个文件写入
splitfilelist[i%num].write(line)
i += 1
bigfile.close()
# 3.关闭文件
for splitfile in splitfilelist:
splitfile.close()