用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同步两个文件夹下的内容,供大家参考,具体内容如下 import os import shutil import time import logging...

Python实现的计算马氏距离算法示例

Python实现的计算马氏距离算法示例

本文实例讲述了Python实现的计算马氏距离算法。分享给大家供大家参考,具体如下: 我给写成函数调用了 python实现马氏距离源代码: # encoding: utf-8 fro...

详解Python连接MySQL数据库的多种方式

上篇文章分享了windows下载mysql5.7压缩包配置安装mysql 后续可以选择 ①在本地创建一个数据库,使用navicat工具导出远程测试服务器的数据库至本地,用于学习操作,且不...

Python中类的定义、继承及使用对象实例详解

本文实例讲述了Python中类的定义、继承及使用对象的方法。分享给大家供大家参考。具体分析如下: Python编程中类的概念可以比作是某种类型集合的描述,如“人类”可以被看作一个类,然后...

11月编程语言排行榜 Python逆袭C#上升到第4

11月编程语言排行榜 Python逆袭C#上升到第4

TIOBE 11 月编程语言排行榜,Python 逆袭C# 曾经有一段时间,脚本语言因其易于编写和易于运行的特性,被预测在未来将发展强大。因此,Perl,Python,PHP 和 Rub...