Use `with open('%s/%s' % (bdir, fn), 'w') as fp:`, and it will automatically be closed (even in the case of exceptions)