In this paper, we describe how Python implements split files and merge files. Share to everyone for your reference. Specific as follows:
The split file split.py is as follows:
#!/usr/bin/python########################################################################### split a file into a set of parts; Join.py puts them back together;# this was 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 is 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.exist S (todir): # Caller handles Errors Os.mkdir (todir) # make dir, read/write parts else:for fname in Os.listdir (t Odir): # Delete any existing files Os.remove (Os.path.join (Todir, fname)) partnum = 0 input = open (FromFile, ' RB ') # Use binary mode on Windows 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.clo SE () # or simply open (). Write () Input.close () assert Partnum <= 9999 # Join sort fails if 5 digits return p ARTNUMIF __name__ = = ' __main__ ': If Len (sys.argv) = = 2 and sys.argv[1] = = '-help ': print ' Use:split.py [File-to-split ta Rget-dir [chunksize]] ' Else:if len (sys.argv) < 3:interactive = 1 FromFile = raw_input (' File to is split? ') # input if clicked Todir = Raw_input (' Directory to store ' files? ') else:interactive = 0 FromFile, Todir = sys.argv[1:3] # args in CmdLine if Len (sys.argv) = = 4:chunksize = I NT (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 ' in ' , Absto if Interactive:raw_input (' Press Enter key ') # pause if clicked
The merged files are join_file.py as follows:
#!/usr/bin/python########################################################################### join all the files in A dir created by split.py, to recreate file. # This was roughly like a ' cat fromdir/* > ToFile ' command on UNIX, but was # more portable and configurable, and exports The join operation as a # reusable function. Relies on sort order of file names:must is 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 (fromdi R) 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 is recreated? ') else:interactive = 0 Fromdir, ToFile = sys.argv[1:] absfrom, Absto = Map (Os.path.abspath, [Fromdir, ToFile]) pr int ' 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
Hopefully this article will help you with Python programming.