`dirname` will be an empty string if you call `os.path.dirname` on a file and not a directory.

To correctly handle this case I would change the if to:

```python
if dirname and not os.path.exists(dirname)
```