Posts Tagged ‘exception’

for / else and try / except / else

June 17, 2016 1 comment

What is that “else” in a for loop? And that “else” in an exception handler?

They can be confusing but in this thread I found a perfect way to remember what they mean. Asdayasman suggests that we should always annotate these “else” branches:

for _ in []:
else:  # nobreak

else:  # noexcept

To be honest, IMO it is best to avoid for / else completely.

Categories: python Tags: , , ,

ignore a specific OSError error

October 17, 2015 2 comments

I have a Google Nexus tablet and its file system is available via the MTP protocol. I can access the tablet’s file system in command-line but when I want to copy a file to it with a Python script, I get this error:

  File "", line 81, in create_in_dest
    shutil.copy(src_long, dest_long)
  File "/usr/lib/python3.5/", line 236, in copy
    copymode(src, dst, follow_symlinks=follow_symlinks)
  File "/usr/lib/python3.5/", line 138, in copymode
    chmod_func(dst, stat.S_IMODE(st.st_mode))
OSError: [Errno 95] Operation not supported ...

It means that the permissions of the original file cannot be set on the destination file. The file is copied but then this error is raised. If the permissions are not the same, who cares? Not a big deal. So how to ignore this specific error (Errno 95)?


import errno
import shutil

def my_copy(src, dest):
        shutil.copy(src, dest)
    except OSError as e:
        if e.errno == 95:

This error code 95 could also be written as errno.ENOTSUP (it’s a constant in the errno module).