批量創建文件其實很簡單,只需要按照需要創建寫文件、寫完關閉當前寫文件、創建新的寫文件、寫完關閉當前文件、、、不斷循環即可,以下是一個簡單例子,將大文件big.txt按照每1000行分割成一個個小文件,具體做法如下:
# -*- coding: utf-8 -*-
index = 0
count = 0
f_in = open("%d.txt" % index, "w")
with open("big.txt", "r") as f_out:
for line in f_out:
count += 1
f_in.write(line)
# 讀滿1000行之後,行計數置零,小文件序號加一,創建一個新的文件寫信息
if count == 1000:
f_in.close()
count = 0
index += 1
f_in = open("%d.txt" % index, "w")