PSE Entertainment Corp
February 06, 2012, 03:01:57 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Enjoy PSE Tunes!  They're great!
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Python File Splitter / Combiner  (Read 888 times)
webmast
Guest
« on: May 24, 2009, 08:26:57 AM »

The following command line python application will take a file of any size and split it into X number of components.  This same tool will combine those file back into the original.  The only limitation on file size is system memory - if you split a 10GB file into two peices, you'd better have 5GB of RAM to read each component!

Code:
import sys, os, glob

def usage():
    print """
****************************************************
PY_FILESPLITTER

Copyright: PSE Entertainment Corp 2009 (all rights reserved)

This utility will split and/or concatenate any file you wish.

Usage:  py_file_splitter [action] [filename] [num_files]

Options:
    action - combine or split
    filename - the full file name
    num_files - if the action is split, this is the number of files to create

Output:
    The split files will be created and numbered in the same folder as the original.

example:  py_file_splitter split blah.jpg 3
result: blah.jpg.1, blah.jpg.2, blah.jpg.3

example:  py_file_splitter combine blah.jpg
result:  blah.jpg

NOTE: this usage will appear with any errors - check your spelling!
*******************************************************
"""
    sys.exit()

def splitter(file_name,split_num):
    print
    print "Splitting File %s Into %s Chunks" % (file_name, split_num)
    try:
        file = open(file_name,'rb')
    except:
        print "no such file..."
        usage()
    size = os.stat(file_name).st_size
    file_chunks,remainder = divmod(size,split_num)[0],divmod(size,split_num)[1]
    print "%s is %s bytes.  Each chunk will be about %s bytes." % (file_name,size,file_chunks)
    print "...Do you have enough memory to do this?..."
    question = raw_input("""Do you wish to continue? (y/n): """)
    if question.lower == 'n':
        sys.exit()
    for i in range(split_num):
        if i < split_num-1:
            chunk = file.read(file_chunks)
            outfile = open("%s.%s" % (file_name,i),'wb')
            outfile.write(chunk)
            print "Created File: %s.%s" % (file_name,i)
            outfile.close()
        else:
            chunk = file.read(file_chunks+remainder)
            outfile = open("%s.%s" % (file_name,i),'wb')
            outfile.write(chunk)
            print "Created File: %s.%s" % (file_name,i)
            outfile.close()
    print "Finished"
    file.close()

def concatenate(file_name):
    print "\nCombining..."
    print "File: %s" % (file_name)
    files = glob.glob('%s.[0-9]*' % file_name)
    if len(files) < 1:
        print "no such file..."
        usage()
    #################Sort the glob in a nice human way
    import re
    def tryint(s):
        try:
            return int(s)
        except:
            return s
    def alphanum_key(s):
        """ Turn a string into a list of string and number chunks.
            "z23a" -> ["z", 23, "a"]
        """
        return [ tryint(c) for c in re.split('([0-9]+)', s) ]
    def sort_nicely(l):
        """ Sort the given list in the way that humans expect.
        """
        l.sort(key=alphanum_key)
    sort_nicely(files)
    #################
    print files      
    outfile = open(file_name,'wb')
    for x in files:
        blah = open(x,'rb')
        data = blah.read()
        blah.close()
        outfile.write(data)
        print "Added File: %s" % x
    outfile.close()
    print "Completed..."


if __name__ == '__main__':
    try:
        action = sys.argv[1].lower()
    except: usage()
      
    if action == "combine":
        try:
            file_name = sys.argv[2]
        except: usage()
        concatenate(file_name)
        sys.exit()
        
    if action == "split":
        try:
            file_name = sys.argv[2]
            split_num = int(sys.argv[3])
        except: usage()
        splitter(file_name,split_num)
        sys.exit()
        
    else: usage()
« Last Edit: May 01, 2010, 11:00:35 AM by webmaster » Report to moderator   Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.14 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!