用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文件读写操作与linux shell变量命令交互执行的方法

本文实例讲述了python文件读写操作与linux shell变量命令交互执行的方法。分享给大家供大家参考。具体如下: python对文件的读写还是挺方便的,与linux shell的交...

python使用turtle库与random库绘制雪花

python使用turtle库与random库绘制雪花

本文实例为大家分享了python绘制雪花的具体代码,供大家参考,具体内容如下 代码非常容易理解,画着玩玩还是可以的。直接上代码 # -*- coding: utf-8 -*- """...

基于python内置函数与匿名函数详解

基于python内置函数与匿名函数详解

内置函数 Built-in Functions abs() dict() help()...

Python中生成器和迭代器的区别详解

Python中生成器和迭代器的区别(代码在Python3.5下测试): Num01–>迭代器 定义: 对于list、string、tuple、dict等这些容器对象,使用for循环...

基于pandas数据样本行列选取的方法

注:以下代码是基于python3.5.0编写的 import pandas food_info = pandas.read_csv("food_info.csv") # ------...