rikxik has the easier way to do it.
1. eval is an in-built python function. works sort of like C pointers. sort of.
2. in Python, %s in a string followed by % variable outside of the string (but in the same expression) places the given variable as text into the string.
outfile = open('%s' % filename,'w')
is the same as
outfile = open(b,'w')
but its a habbit of mine to have a variable serve only 1 purpose in a program so that it can be deleted immediately after it is used.