用python分割TXT文件成4K的TXT文件
程序员文章站
2022-05-14 08:59:18
复制代码 代码如下:########################## # # # 为了避免截断中文字符 # # 文件要求是 unicode 编码 # # txt文件另存...
复制代码 代码如下:
##########################
# #
# 为了避免截断中文字符 #
# 文件要求是 unicode 编码 #
# txt文件另存为对话框下面有下拉框,可选存 #
# 储编码格式 #
# #
##########################
import os
import struct
filename = str(raw_input("please enter an old file name: "))
filenamepre = str(raw_input("please enter an new file name prefix: "))
count = 0
filecount = 0
maxcount = 20
newfilename = repr(filecount) + '.txt'
oldfile = open(filename,'rb')
bfirst = true
while true:
s = oldfile.read(512*8 - 4)
if not s:
exit()
filecount = filecount + 1
newfilename = filenamepre + repr(filecount).zfill(2) + '.txt'
newfile = open(newfilename,'wb')
if not bfirst:
be = 0xfeff
newfile.write(struct.pack('h',be))
newfile.write(s)
be = 0x000a000d
newfile.write(struct.pack('i',be))
newfile.close()
bfirst = false
oldfile.close()