Can you replace this whole thing with
```python
with codecs.open(stylefile, 'r', 'utf8') as f:
    css_body = f.read()
```
If the line `f = codecs.open(stylefile)` fails for some reason, then variable `f` never gets assigned and then, when in the `finally:` block, `f` doesn't exist so you get a `NameError`.