3

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.
Sun Bear
  • 7,594
  • 11
  • 56
  • 102

1 Answers1

8

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--'
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • At a simpler level, is mode suppose to be a 5 digit number like 33204 or a 8 digit character like 0o000600 or a text like shown [here'](https://docs.python.org/3/library/stat.html#stat.S_IXUSR)? – Sun Bear Jul 30 '19 at 04:04
  • @SunBear The number can be any number depending on file mode (0 ~ 4xxxx). (because mode is combination of bits listed in the documentation). BTW `33204` is a decimal representation, `0o100664` is a octal representation. – falsetru Jul 30 '19 at 04:07
  • Do you mean I can enter either decimal or octal representation for mode? Is there a command that can convert '-rw-rwxr--' to decimal or octal representation? If not, how do I manually convert it? – Sun Bear Jul 30 '19 at 04:10
  • @SunBear, You can use bit-wise OR operator: `|` to combine mode bits: `0o600 | 0o70 | 0o4` or `0o674` directly to mean `-rw (6 = 4|2) -rwx (7 = 4|2|1) r-- (4)` – falsetru Jul 30 '19 at 04:22
  • The python interpreter returned '444' when I submitted `0o600 | 0o70 | 0o4`. After resubmiting `p.chmod( 444 )`, `p.stat().st_mode` returned 33212. I got what I wanted (confirmed by checking the permission of the file. I also tried `p.chmod( 33212 )` and got the permission I wanted. But why did these two decimal representatives (444 and 33212) yield the same response? – Sun Bear Jul 30 '19 at 05:16
  • @SunBear, If you don't specify file type (S_IFREG 0o100000) in this case it is set by default. Try oct(444) oct(33212). Then, you will know what I mean. – falsetru Jul 30 '19 at 09:37
  • I made a mistake in my earlier comment. I tried both `p.chmod(444)` and `p.chmod(33212)` but I keep getting `-rwxrwxr-x` and not `-rw-rwxr--`. Can you explain why I can't get `-rw-rwxr--` and how to resolve this issue? Thanks. In bash, `chmod 674 filename.ext`did give me `-rw-rwxr--`. – Sun Bear Aug 06 '19 at 02:36
  • @SunBear, `-rwxrwxr-x` -> `0o775` == `559`. If you're not familiar with octal representation, I recommend you to study octal/decimal/hexadecimal representation. (`chmod` command use octal representation.) – falsetru Aug 06 '19 at 02:52