Look at the "return" statements in this function:  one returns "text/plain", one returns an open file object, and one returns "404 Not Found".   Inconsistent, don't you think?  One function should always return the same "type" of value.  In this case, you should return a two-tuple of (body, mimetype).