The register %
contains the current buffer name. Somehow, you seem to be in the scratch buffer that NERDTree uses to display its directory tree when the error occurs.
You should have noticed that the cursor is in the "wrong" (i.e. non-Python) buffer. If this happens in a Python buffer, I cannot explain that.
You can make your mapping more robust to include a check for Python filetype, like this:
... if &filetype ==# 'python'<Bar>update<Bar>execute '!python '.shellescape(@%, 1)<Bar>endif<CR>