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-2 of 2 total  RSS 

fractionfiles.py

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

#!/usr/bin/env python

"""Splits and joins files. Helpful when media can't fit a file.
Be prepared for a lot of output files!"""

__author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)"
__date__="6 Jan 3006 - 12 Feb 2006"
__copyright__="Copyright 2006 Andrew Pennebaker"
__license__="GPL"
__version__="0.3"
__URL__="http://snippets.dzone.com/posts/show/3541"

import sys, os
from getopt import getopt

SPLIT_MODE="SPLIT"
JOIN_MODE="JOIN"

def splitFile(name, length, number):
	if length==None:
		infile=open(name, "rb")
		size=0
		while infile.read(1)!="":
			size+=1

		infile.close()

		maxlength=size/number
		if number*maxlength<size:
			maxlength+=1

	else:
		if length<1:
			raise Exception

	infile=None
	try:
		infile=open(name, "rb")
	except Exception, e:
		raise e

	i=0
	j=0
	c=infile.read(1)
	while c!="":
		outfile=None
		try:
			outfile=open("%s.%d" % (name, j), "wb")
		except Exception, e:
			raise e

		while i<length and c!="":
			outfile.write(c)
			c=infile.read(1)
			i+=1

		outfile.close()
		i=0
		j+=1

	infile.close()

def joinFiles(filenames):
	if len(filenames)<1:
		raise Exception

	filenames.sort() # ...0 must be first

	origFilename=filenames[0][0:-2] # take ".0" off the first file name
	origFile=None

	try:
		origFile=open(origFilename, "wb")
	except Exception, e:
		raise e

	c="&" # dummy

	for filename in filenames:
		smallFile=None
		try:
			smallFile=open(filename, "rb")
		except Exception, e:
			raise e

		c=smallFile.read(1)
		while c!="":
			origFile.write(c)
			c=smallFile.read(1)

		smallFile.close()

	origFile.close()

def usage():
	print "Usage: %s [options] [files]" % (sys.argv[0])
	print "\n--split <file1 file 2 file 3...>"
	print "--join <dir1 dir2 dir3 ...>"
	print "--maxlength <bytes>"
	print "--maxfiles <number>"
	print "--help (usage)"

	sys.exit()

def main():
	global SPLIT_MODE
	global JOIN_MODE

	mode=SPLIT_MODE
	filenames=[]
	maxlength=1024
	maxfiles=None

	systemArgs=sys.argv[1:] # ignore program name

	optlist=[]
	args=[]

	try:
		optlist, args=getopt(systemArgs, None, ["split", "join", "maxlength=", "maxfiles=", "help"])
	except Exception, e:
		usage()

	if len(optlist)<1 or len(args)<1:
		usage()

	for option, value in optlist:
		if option=="--help":
			usage()

		elif option=="--split":
			mode=SPLIT_MODE
		elif option=="--join":
			mode=JOIN_MODE
		elif option=="--maxlength":
			try:
				maxlength=int(value)
				if maxlength<1:
					raise Exception
				maxfiles=None
			except Exception, e:
				raise "Length must be at least one"
		elif option=="--maxfiles":
			try:
				maxfiles=int(value)
				if maxfiles<1:
					raise Exception
				maxlength=None
			except Exception, e:
				raise "Number must be at least one"

	filenames=args

	if mode==SPLIT_MODE:
		for filename in filenames:
			try:
				splitFile(filename, maxlength, maxfiles)
			except Exception, e:
				raise e

	elif mode==JOIN_MODE:
		for directory in filenames:
			files=["%s%s%s" % (directory, os.sep, file) for file in os.listdir(directory)]

			try:
				joinFiles(files)
			except Exception, e:
				raise e

if __name__=="__main__":
	main()

downloader.py

#!/usr/bin/env python

__author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)"
__date__="3 Nov 2005 - 14 Feb 2007"
__copyright__="Copyright 2006 2007 Andrew Pennebaker"
__license__="GPL"
__version__="0.5"
__URL__="http://snippets.dzone.com/posts/show/2887"

from urllib import urlopen

import os

import sys
from getopt import getopt

def getURLName(url):
	directory=os.curdir

	name="%s%s%s" % (
		directory,
		os.sep,
		url.split("/")[-1]
	)

	return name

def createDownload(url, proxy=None):
	instream=urlopen(url, None, proxy)

	filename=instream.info().getheader("Content-Length")
	if filename==None:
		filename="temp"

	return (instream, filename)

def download(instream, outstream):
	outstream.write(instream.read())

	outstream.close()

def usage():
	print "Usage: %s [options] <url1 url2 url3 ...>" % (sys.argv[0])
	print "\n--httpproxy <proxy>"
	print "--ftpproxy <proxy>"
	print "--gopherproxy <proxy>"
	print "\n--help (usage)"

	sys.exit()

def main():
	systemArgs=sys.argv[1:] # ignore program name

	urls=[]
	proxies={}

	optlist=[]
	args=[]

	try:
		optlist, args=getopt(systemArgs, None, ["url=", "httpproxy=", "ftpproxy=", "gopherproxy=", "help"])
	except Exception, e:
		usage()

	if len(args)<1:
		usage()

	for option, value in optlist:
		if option=="--help":
			usage()

		elif option=="--httpproxy":
			proxies["http"]=value
		elif option=="--ftpproxy":
			proxies["ftp"]=value
		elif options=="--gopherproxy":
			proxies["gopher"]=value

	urls=args

	for url in urls:
		try:
			outfile=open(getURLName(url), "wb")
			fileName=outfile.name.split(os.sep)[-1]

			url, length=createDownload(url, proxies)
			if not length:
				length="?"

			print "Downloading %s (%s bytes) ..." % (url.url, length)
			if length!="?":
				length=float(length)
			bytesRead=0.0

			for line in url:
				bytesRead+=len(line)

				if length!="?":
					print "%s: %.02f/%.02f kb (%d%%)" % (
						fileName,
						bytesRead/1024.0,
						length/1024.0,
						100*bytesRead/length
					)

				outfile.write(line)

			url.close()
			outfile.close()
			print "Done"

		except Exception, e:
			print "Error downloading %s: %s" % (url, e)

if __name__=="__main__":
	main()
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS