f.close was invoked even when the file was not open

Signed-off-by: Luca Clementi <luca.clementi@gmail.com>