Archive

Archive for October, 2015

ignore a specific OSError error

October 17, 2015 2 comments

Problem
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 "something.py", line 81, in create_in_dest
    shutil.copy(src_long, dest_long)
  File "/usr/lib/python3.5/shutil.py", line 236, in copy
    copymode(src, dst, follow_symlinks=follow_symlinks)
  File "/usr/lib/python3.5/shutil.py", 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)?

Solution

import errno
import shutil

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

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

Advertisements

Anaconda: problem with the readline module under Manjaro

October 5, 2015 Leave a comment

Problem
I tried the Anaconda Python distribution under Manjaro, but there was a problem with the “readline” module:

>>> import readline
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: libncursesw.so.5: cannot open shared object file: No such file or directory

Solution
As root:

# cd /usr/lib
# ln -s libncursesw.so libncursesw.so.5

It may be an ugly hack, but it solved the issue for me.

Categories: python Tags: , ,