instead of f = file(...) ... f.close(), you should use the "with" statement as close will be called even if you have exception.