国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實(shí)現(xiàn)分割文件及合并文件的方法

2019-11-25 17:12:10
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Python實(shí)現(xiàn)分割文件及合并文件的方法。分享給大家供大家參考。具體如下:

分割文件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, oskilobytes = 1024megabytes = kilobytes * 1000chunksize = int(1.4 * megabytes)     # default: roughly a floppydef 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 partnumif __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, sysreadsize = 1024def 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

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 逊克县| 长顺县| 百色市| 宁强县| 汝城县| 文登市| 汉沽区| 防城港市| 海阳市| 民乐县| 安仁县| 普兰县| 张掖市| 那坡县| 扎鲁特旗| 遵化市| 阿城市| 鲁甸县| 西乡县| 东阳市| 敖汉旗| 琼海市| 永善县| 东丰县| 汝州市| 筠连县| 神木县| 旬阳县| 西昌市| 英吉沙县| 乌海市| 河津市| 原平市| 岳西县| 临武县| 普兰县| 芦山县| 比如县| 台南县| 嵊州市| 五大连池市|