pythonmacosmacos-catalinachmodpython-os

OSError: [Errno 30] Read-only file system: '/User'. macOS Catalina


I was coding sorter for downloads folder. I'm getting this error, I tried to change permissions:
chmod: Unable to change file mode on Users: Operation not permitted

import os

from_dir = os.path.dirname('/Users/user/Downloads/')
working_dir = os.walk(from_dir)
to_dir = os.path.dirname('/User/user/Downloads/New Folder/')


def move(folder):
    for roots, dirs, files in folder:
        for file in files:
            src_folder = from_dir + '/' + file
            to_folder = to_dir + '/' + file
            if not os.path.exists(to_dir):
                os.makedirs(to_dir)
            os.rename(src_folder, to_folder)


move(working_dir)

Maybe there is another way to write this code just without touching root folders?

Full error:

Traceback (most recent call last):
  File "/Users/beknazarnurbek/Documents/PycharmProjects/Move Files/move.py", line 19, in <module>
    move(working_dir)
  File "/Users/beknazarnurbek/Documents/PycharmProjects/Move Files/move.py", line 14, in move
    os.makedirs(to_dir)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/os.py", line 211, in makedirs
    makedirs(head, exist_ok=exist_ok)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/os.py", line 211, in makedirs
    makedirs(head, exist_ok=exist_ok)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/os.py", line 211, in makedirs
    makedirs(head, exist_ok=exist_ok)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/os.py", line 221, in makedirs
    mkdir(name, mode)
OSError: [Errno 30] Read-only file system: '/User'

Solution

  • That error message is bit misleading. In this case, the problem is that there is no /User directory on macOS. The directory is named /Users.

    In the following line

    to_dir = os.path.dirname('/User/user/Downloads/New Folder/')

    User should be Users

    to_dir = os.path.dirname('/Users/user/Downloads/New Folder/')

    What's happening is that os.mkdirs() is attempting to create a directory User in /. Which is not writable. Which is causing the error message.