#!/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 the 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 and to recreate file. # This are roughly like a ' cat fromdir/* > ToFile ' command on UNIX, but are # 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, 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 |