Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation on mode in pathlib.Path.chmod(mode) [duplicate]

May I know how to define the mode in pathlib.Path.chmod(mode). I did not find any explanation or explanation links on how to define mode in python 3.6 documentation. E.g.

>>> p = Path( 'filename.ext' )
>>> p.stat().st_mode
33204

What is the meaning of the five digits either individually or together? I would like to change the value to so that Owner has execute permission. How do I work out the values to use for mode?

Alternative Solution:

I like to thank @falsetru for his answer and comments. Also, I like to share a non mathematical approach to find the "mode value" of a desired permission level that can be submitted to a pathlib.Path.chmod(mode) command.

Here are the Steps:

  1. Decide on the permission levels you want for the file.
  2. Use a file manager (e.g. nautilus) to select the file, then right-click on it, click on "Properties" followed by left-clicking on the "Permission" Tab. Here you can set the desired permission levels for the file.
  3. Next, from a Python interpreter, submit the above mentioned commands. It will return the corresponding mode value for the permission level you want. You can then use that in pathlib.Path.chmod(mode) command.
like image 873
Sun Bear Avatar asked Sep 05 '25 20:09

Sun Bear


1 Answers

If you follow the link (os.chmod), you will know each bit means.

By converting the mode value to octal representation, it would be easier to read:

>>> oct(33204)
'0o100664'
  • regular file: 0o100000 (33204 & S_IFREG -> non-zero OR S_ISREG(33204) -> True) S_IFREG, S_ISREG
  • read-writable by owner: 0o000600 (rw-)
  • read-writable by group: 0o000060 (rw-)
  • readable by other: 0o000004 (r--)

UPDATE:

stat.filemode converts the number into a human readable format:

>>> stat.filemode(33204)
'-rw-rw-r--'
like image 53
falsetru Avatar answered Sep 08 '25 08:09

falsetru