Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Andrew Pennebaker http://mcandre.devjavu.com/wiki

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

fractionfiles.py

// Splits a file into smaller ones, and joins them together.

   1  
   2  #!/usr/bin/env python
   3  
   4  """Splits and joins files. Helpful when media can't fit a file.
   5  Be prepared for a lot of output files!"""
   6  
   7  __author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)"
   8  __date__="6 Jan 3006 - 12 Feb 2006"
   9  __copyright__="Copyright 2006 Andrew Pennebaker"
  10  __license__="GPL"
  11  __version__="0.3"
  12  __URL__="http://snippets.dzone.com/posts/show/3541"
  13  
  14  import sys, os
  15  from getopt import getopt
  16  
  17  SPLIT_MODE="SPLIT"
  18  JOIN_MODE="JOIN"
  19  
  20  def splitFile(name, length, number):
  21  	if length==None:
  22  		infile=open(name, "rb")
  23  		size=0
  24  		while infile.read(1)!="":
  25  			size+=1
  26  
  27  		infile.close()
  28  
  29  		maxlength=size/number
  30  		if number*maxlength<size:
  31  			maxlength+=1
  32  
  33  	else:
  34  		if length<1:
  35  			raise Exception
  36  
  37  	infile=None
  38  	try:
  39  		infile=open(name, "rb")
  40  	except Exception, e:
  41  		raise e
  42  
  43  	i=0
  44  	j=0
  45  	c=infile.read(1)
  46  	while c!="":
  47  		outfile=None
  48  		try:
  49  			outfile=open("%s.%d" % (name, j), "wb")
  50  		except Exception, e:
  51  			raise e
  52  
  53  		while i<length and c!="":
  54  			outfile.write(c)
  55  			c=infile.read(1)
  56  			i+=1
  57  
  58  		outfile.close()
  59  		i=0
  60  		j+=1
  61  
  62  	infile.close()
  63  
  64  def joinFiles(filenames):
  65  	if len(filenames)<1:
  66  		raise Exception
  67  
  68  	filenames.sort() # ...0 must be first
  69  
  70  	origFilename=filenames[0][0:-2] # take ".0" off the first file name
  71  	origFile=None
  72  
  73  	try:
  74  		origFile=open(origFilename, "wb")
  75  	except Exception, e:
  76  		raise e
  77  
  78  	c="&" # dummy
  79  
  80  	for filename in filenames:
  81  		smallFile=None
  82  		try:
  83  			smallFile=open(filename, "rb")
  84  		except Exception, e:
  85  			raise e
  86  
  87  		c=smallFile.read(1)
  88  		while c!="":
  89  			origFile.write(c)
  90  			c=smallFile.read(1)
  91  
  92  		smallFile.close()
  93  
  94  	origFile.close()
  95  
  96  def usage():
  97  	print "Usage: %s [options] [files]" % (sys.argv[0])
  98  	print "\n--split <file1 file 2 file 3...>"
  99  	print "--join <dir1 dir2 dir3 ...>"
 100  	print "--maxlength <bytes>"
 101  	print "--maxfiles <number>"
 102  	print "--help (usage)"
 103  
 104  	sys.exit()
 105  
 106  def main():
 107  	global SPLIT_MODE
 108  	global JOIN_MODE
 109  
 110  	mode=SPLIT_MODE
 111  	filenames=[]
 112  	maxlength=1024
 113  	maxfiles=None
 114  
 115  	systemArgs=sys.argv[1:] # ignore program name
 116  
 117  	optlist=[]
 118  	args=[]
 119  
 120  	try:
 121  		optlist, args=getopt(systemArgs, None, ["split", "join", "maxlength=", "maxfiles=", "help"])
 122  	except Exception, e:
 123  		usage()
 124  
 125  	if len(optlist)<1 or len(args)<1:
 126  		usage()
 127  
 128  	for option, value in optlist:
 129  		if option=="--help":
 130  			usage()
 131  
 132  		elif option=="--split":
 133  			mode=SPLIT_MODE
 134  		elif option=="--join":
 135  			mode=JOIN_MODE
 136  		elif option=="--maxlength":
 137  			try:
 138  				maxlength=int(value)
 139  				if maxlength<1:
 140  					raise Exception
 141  				maxfiles=None
 142  			except Exception, e:
 143  				raise "Length must be at least one"
 144  		elif option=="--maxfiles":
 145  			try:
 146  				maxfiles=int(value)
 147  				if maxfiles<1:
 148  					raise Exception
 149  				maxlength=None
 150  			except Exception, e:
 151  				raise "Number must be at least one"
 152  
 153  	filenames=args
 154  
 155  	if mode==SPLIT_MODE:
 156  		for filename in filenames:
 157  			try:
 158  				splitFile(filename, maxlength, maxfiles)
 159  			except Exception, e:
 160  				raise e
 161  
 162  	elif mode==JOIN_MODE:
 163  		for directory in filenames:
 164  			files=["%s%s%s" % (directory, os.sep, file) for file in os.listdir(directory)]
 165  
 166  			try:
 167  				joinFiles(files)
 168  			except Exception, e:
 169  				raise e
 170  
 171  if __name__=="__main__":
 172  	main()
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS