本文实例讲述了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"htmlcode">#!/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程序设计有所帮助。
标签:
Python,分割文件,合并文件
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“Python实现分割文件及合并文件的方法”评论...
更新动态
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]