This is probably a stupid question, but I just can’t figure out how to simplify this code.
I have a web form that contains eleven file upload buttons.
These are tagged p1-3, q1-5, x1-2, and cb.
In the script that processes the uploads, I need to check if a new file is being uploaded, or if just one (or none) of them is being changed.
If it’s changed, I save it and create an ogg to use in an audio tag.
Here’s the question. How do I iterate through the variable names cleanly? Right now I’ve got eleven blocks like this, and it makes me cringe.
I suppose simply creating a function to handle everything after the if:raise is easy enough, but what I’d like to do is simply pass a function a list of the names that I’m looking for, have it assign a variable, and take care of business.
try:
x2 = form['x2']
if not x2.filename: raise
outfile = '%s/x2.wav' % savepath
oggfile = '%s/x2.ogg' % oggdir
open(outfile, 'wb').write(x2.file.read())
command = 'oggenc %s -o %s' % (outfile, oggfile)
output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
except:
pass
Try something like the following: