用python分割TXT文件成4K的TXT文件

yipeiwu_com6年前Python基础
复制代码 代码如下:

##########################
# #
# 为了避免截断中文字符 #
# 文件要求是 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()

相关文章

利用python生成一个导出数据库的bat脚本文件的方法

实例如下: # 环境: python3.x def getExportDbSql(db, index): # 获取导出一个数据库实例的sql语句 sql = 'mysqldu...

Linux上使用Python统计每天的键盘输入次数

Github 项目主页 工具源码 分析结果: total : 15981 1568.0 == Backspace 1103.0 == Tab 1038.0 == Enter 900....

使用python分析git log日志示例

用git来管理工程的开发,git log是非常有用的‘历史'资料,需求就是来自这里,我们希望能对git log有一个定制性强的过滤。此段脚本就是在完成这种类型的任务。对于一个repo所有...

Pytorch to(device)用法

如下所示: device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model.to(devi...

Python3.4 splinter(模拟填写表单)使用方法

如下所示: from splinter.browser import Browser b = Browser('chrome') url = 'https://kyfw.12...