4

I want to change permission of file inside python file system (pyfilesystem).

Here's the code I have:

fslocal = fs.osfs.OSFS(localdir, create=True, thread_synchronize=True)
fslocal.setcontents('/file1', b'This is file 1')

Now I want to change file permission of file 1. I am using os.chmod for this

os.chmod(localdir + '/file1', stat.S_IWOTH)

However, I get this error:

Traceback (most recent call last):
File "/Users/user/.conda/envs/scram/lib/python3.4/site-packages/fs/errors.py", line 257, in wrapper

return func(self,*args,**kwds)

File "/Users/user/.conda/envs/scram/lib/python3.4/site-packages/fs/osfs/__init__.py", line 251, in listdir

listing = os.listdir(sys_path)

PermissionError: [Errno 13] Permission denied: '/Users/user/Documents/tests/function/arwan/localfs/file1

Can you please tell me if it is possible to do it and how?

Thanks.

2
  • Try launching this with sudo. Commented Oct 21, 2015 at 14:15
  • I think the problem is not on permission effected by sudo command. Instead, it is a permission because we put the file inside a file system. Anyway, thanks for your answer. Commented Oct 22, 2015 at 8:16

1 Answer 1

3

One problem is that it appears you're calling chmod with the intent of adding a single permission bit. In fact, you are setting all of the permission bits, so the call is trying to clear all of them except the one you want set. Assuming you're on a Unix system, you will presumably want to set the user and group bits as well, including the read and execute bits.

You can do the following:

st = os.stat(path)
old_mode = st.st_mode
new_mode = old_mode | stat.S_IWOTH
os.chmod(path, new_mode)

Hopefully that will help you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.