8

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'
Harshit Ruwali
  • 1,040
  • 2
  • 10
  • 22

5 Answers5

2

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.

teebly
  • 23
  • 6
0

To add to the already existing answers: You cannot write to the "input" directory as it is read-only. So you will have to write to Another directory , like the one before the current one.

instead of writing to user/local/current try to right to directory before current which is local in this case.

Pedram
  • 53
  • 6
0

Just change '/static/' to 'static/' and 'media/' to 'media' in the settings.

Zafer
  • 2,180
  • 16
  • 28
DK_521
  • 1
  • 3
-1

You have set writable permission to this folder. Just use in terminal chmod:

chmod -R 777 /absolute/path/to/your/folder
  • 19
    Whatever you are hoping to accomplish, **`chmod 777` is *wrong* and *dangerous.*** You will want to revert to sane permissions ASAP (for your use case, probably `chmod 755`) and if you have had world writable system files on a public-facing system, at the very least investigate whether it could have been breached and used as a pivot point for breaking into your organization’s network. – tripleee Nov 08 '21 at 12:27
-2

Just removed os.path.dirname and it worked.

After I removed os.path.dirname it worked, but not how I was expecting. then trying to fix this, I messed up file system permission and just reinstalled OS. Now it works fine.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • 3
    So then this is not really an answer to the question. It seems to me that the actual problem was that your file system was mounted read-only (as mentioned in the error message) and reinstalling the OS apparently solved your problem. BTW: reinstalling was overkill, you probably could have changed the mounting options to solve the problem without a complete reinstall. – wovano Jul 26 '20 at 09:48