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

yipeiwu_com5年前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中类的创建和实例化操作。分享给大家供大家参考,具体如下: python中同样使用关键字class创建一个类,类名称第一个字母大写,可以带括号也可以不带括号; p...

一篇文章彻底搞懂Python中可迭代(Iterable)、迭代器(Iterator)与生成器(Generator)的概念

前言 在Python中可迭代(Iterable)、迭代器(Iterator)和生成器(Generator)这几个概念是经常用到的,初学时对这几个概念也是经常混淆,现在是时候把这几个概念搞...

使用Python开发windows GUI程序入门实例

今天终于可以用wxPython开发GUI程序了,非常高兴。把其中的一些注意点写下来以供参考。在windows XP平台下,首先需要做以下环境的配置: 1. 首先是安装python ,安装...

基于python进行桶排序与基数排序的总结

本文首先举例阐述了两种排序方法的操作步骤,然后列出了用python进行的实现过程,最后对桶式排序方法的优劣进行了简单总结。 一、桶排序: 排序一个数组[5,3,6,1,2,7,5,10]...

python统计多维数组的行数和列数实例

python菜鸟,每天都要进步一点点。 二维元组的例子: A = ((1, 1, 1), (1, 1, 1),(1, 1, 1),(0, 0, 0)) print len(A) #...