本文为大家讲解分析用Python来将一个大的txt文件分割成需要的大小k,论文,Python读取、分割、压缩TXT文件的方法代码详解。大文本文件我们可能只需要其中一小部分,这时候就需要来做分割文件的操作,用Python代码也可以很好的完成。
实现文件分割需要用到的python模块方法有Python os模块、struct,具体代码如下:
''' 为了避免截断中文字符 文件要求是 unicode 编码 txt文件另存为对话框下面有下拉框,开题报告,可选存 储编码格式 ''' import os import struct #导入所需python模块方法 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() #文件相关操作切记要关闭浏览此文章的会员还在关注:gzip python
Python其它文件操作方法相关文章:用Python合并多个文件的方法代码、Python 文件对象常用内建方法、Python遍历文件夹目录与文件操作