1

I'm trying to write a tool to work on a USB drive, and the first step is to check if it's mounted and if not, mount it. Every attempt results in an error of "No such file or directory". Working with Python 3.10 in Pycharm 2022.3 on Manjaro 22.0, Linux kernel 5.15.

I am currently using a method given here: https://stackoverflow.com/a/29156997/15763354 adapted as follows:

os.mkdir('/run/media/myname')
libc = ctypes.CDLL(ctypes.util.find_library('c'), use_errno=True)
libc.mount.argtypes = (ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_ulong, ctypes.c_char_p)
    def mount(source, target, fs, options=''):
        ret = libc.mount(source.encode(), target.encode(), fs.encode(), 0, options.encode())
        if ret < 0:
            errno = ctypes.get_errno()
            raise OSError(errno,
                          f"Error mounting {source} ({fs}) on {target} with options '{options}': {os.strerror(errno)}")
    mount('/dev/sdb1', '/run/media/myname', 'fat32', 'rw')

and I get the error:

FileNotFoundError: [Errno 2] Error mounting /dev/sdb1 (fat32) on /run/media/myname with options 'rw': No such file or directory

Now I'm not sure which file or directory it's complaining about, but the disk is at sdb1 and when I mount using the file system browser, it mounts to that target location, i.e. /run/media/myname/USB. I've tried different mount targets, tried just using os.system, nothing works, same error.

Any ideas what I might be missing?

Update:

Problem 1: The /run/media/myname directory is not created until a disk is mounted, so step one is to create that directory myself (added above). But that gives rise to...

Problem 2: To create that directory, I need to run PyCharm in sudo. This gets me closer, however my error is now:

SError: [Errno 19] Error mounting /dev/sdb1 (fat32) on /run/media/myname with options 'rw': No such device

which of course is hogwash since I can ls /dev and see sdb1 sitting right there. Also I can see it waiting unmounted in GParted and Thunar.

Any ideas on this one?

  • Please provide information regarding the operating environment. It seems you are running a distro of unix but it is uncertain from what you have provided. – itprorh66 Jan 20 '23 at 16:23
  • Apologies, added. In gathering these details, I noticed PyCharm cannot update because of lack of privileges, which makes me wonder if that's interfering with access to the file system. Investigating... – SnackOverflow Jan 20 '23 at 17:36

0 Answers0