最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

Python实现分割文件及合并文件的方法

来源:懂视网 责编:小采 时间:2020-11-27 14:34:21
文档

Python实现分割文件及合并文件的方法

Python实现分割文件及合并文件的方法:本文实例讲述了Python实现分割文件及合并文件的方法。分享给大家供大家参考。具体如下: 分割文件split.py如下: #!/usr/bin/python ########################################################################## #
推荐度:
导读Python实现分割文件及合并文件的方法:本文实例讲述了Python实现分割文件及合并文件的方法。分享给大家供大家参考。具体如下: 分割文件split.py如下: #!/usr/bin/python ########################################################################## #

本文实例讲述了Python实现分割文件及合并文件的方法。分享给大家供大家参考。具体如下:

分割文件split.py如下:

#!/usr/bin/python
##########################################################################
# split a file into a set of parts; join.py puts them back together;
# this is a customizable version of the standard unix split command-line 
# utility; because it is written in Python, it also works on Windows and
# can be easily modified; because it exports a function, its logic can 
# also be imported and reused in other applications;
##########################################################################
import sys, os
kilobytes = 1024
megabytes = kilobytes * 1000
chunksize = int(1.4 * megabytes) # default: roughly a floppy
def split(fromfile, todir, chunksize=chunksize): 
 if not os.path.exists(todir): # caller handles errors
 os.mkdir(todir) # make dir, read/write parts
 else:
 for fname in os.listdir(todir): # delete any existing files
 os.remove(os.path.join(todir, fname)) 
 partnum = 0
 input = open(fromfile, 'rb') # use binary mode on Windows
 while 1: # eof=empty string from read
 chunk = input.read(chunksize) # get next part <= chunksize
 if not chunk: break
 partnum = partnum+1
 filename = os.path.join(todir, ('part%04d' % partnum))
 fileobj = open(filename, 'wb')
 fileobj.write(chunk)
 fileobj.close() # or simply open().write()
 input.close()
 assert partnum <= 9999 # join sort fails if 5 digits
 return partnum
if __name__ == '__main__':
 if len(sys.argv) == 2 and sys.argv[1] == '-help':
 print 'Use: split.py [file-to-split target-dir [chunksize]]'
 else:
 if len(sys.argv) < 3:
 interactive = 1
 fromfile = raw_input('File to be split? ') # input if clicked 
 todir = raw_input('Directory to store part files? ')
 else:
 interactive = 0
 fromfile, todir = sys.argv[1:3] # args in cmdline
 if len(sys.argv) == 4: chunksize = int(sys.argv[3])
 absfrom, absto = map(os.path.abspath, [fromfile, todir])
 print 'Splitting', absfrom, 'to', absto, 'by', chunksize
 try:
 parts = split(fromfile, todir, chunksize)
 except:
 print 'Error during split:'
 print sys.exc_info()[0], sys.exc_info()[1]
 else:
 print 'Split finished:', parts, 'parts are in', absto
 if interactive: raw_input('Press Enter key') # pause if clicked

合并文件join_file.py如下:

#!/usr/bin/python
##########################################################################
# join all part files in a dir created by split.py, to recreate file. 
# This is roughly like a 'cat fromdir/* > tofile' command on unix, but is 
# more portable and configurable, and exports the join operation as a 
# reusable function. Relies on sort order of file names: must be same 
# length. Could extend split/join to popup Tkinter file selectors.
##########################################################################
import os, sys
readsize = 1024
def join(fromdir, tofile):
 output = open(tofile, 'wb')
 parts = os.listdir(fromdir)
 parts.sort()
 for filename in parts:
 filepath = os.path.join(fromdir, filename)
 fileobj = open(filepath, 'rb')
 while 1:
 filebytes = fileobj.read(readsize)
 if not filebytes: break
 output.write(filebytes)
 fileobj.close()
 output.close()
if __name__ == '__main__':
 if len(sys.argv) == 2 and sys.argv[1] == '-help':
 print 'Use: join.py [from-dir-name to-file-name]'
 else:
 if len(sys.argv) != 3:
 interactive = 1
 fromdir = raw_input('Directory containing part files? ')
 tofile = raw_input('Name of file to be recreated? ')
 else:
 interactive = 0
 fromdir, tofile = sys.argv[1:]
 absfrom, absto = map(os.path.abspath, [fromdir, tofile])
 print 'Joining', absfrom, 'to make', absto
 try:
 join(fromdir, tofile)
 except:
 print 'Error joining files:'
 print sys.exc_info()[0], sys.exc_info()[1]
 else:
 print 'Join complete: see', absto
 if interactive: raw_input('Press Enter key') # pause if clicked

希望本文所述对大家的Python程序设计有所帮助。

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

Python实现分割文件及合并文件的方法

Python实现分割文件及合并文件的方法:本文实例讲述了Python实现分割文件及合并文件的方法。分享给大家供大家参考。具体如下: 分割文件split.py如下: #!/usr/bin/python ########################################################################## #
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top