# coding: utf-8
import os

def swapextensions(dir, before, after):
	if before[:1] != '.':
		before = '.' + before
	thelen = -len(before)
	if after[:1] != '.':
		after = '.' + after
	for path, subdirs, files in os.walk(dir):
		for oldfile in files:
			if oldfile[thelen:] == before:
				oldfile = os.path.join(path, oldfile)
				newfile = oldfile[:thelen] + after
				os.rename(oldfile, newfile)

if __name__ == "__main__":
	import sys
	if len(sys.argv) != 4:
		print "Usage: swapext rootdir before after"
		sys.exit(100)
	swapextensions(sys.argv[1], sys.argv[2], sys.argv[3])
