用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自动保存百度盘资源到百度盘中的实例代码

python自动保存百度盘资源到百度盘中的实例代码

本实例的实现逻辑是,应用selenium UI自动化登录百度盘,读取存储百度分享地址和提取码的txt文档,打开百度盘分享地址,填入提取码,然后保存到指定的目录中 全部代码如下: #...

Python连接mysql数据库的正确姿势

Python连接mysql数据库的正确姿势

Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: GadFly mSQL MySQL PostgreSQL Microsoft SQL S...

对PyQt5中树结构的实现方法详解

对PyQt5中树结构的实现方法详解

树的实质是很多条数据按照一定的内在关系,分层级显示出来。因此每一条数据包括数据项和相互关系。数据项就对应了树中的column,而相互关系对应的是应该显示在哪一个条目下。 PyQt5中,树...

python如何读取bin文件并下发串口

下面是实现代码 # coding:utf-8 import time, serial from struct import * import binascii file = ope...

python根据时间生成mongodb的ObjectId的方法

本文实例讲述了python根据时间生成mongodb的ObjectId的方法。分享给大家供大家参考。具体分析如下: mongodb的_id为ObjectId类型,ObjectId内是包含...