Python module os.chmod(file, 664) does not change the permission to rw-rw-r— but -w--wx----

前端 未结 6 986
轮回少年
轮回少年 2020-12-13 01:42

Recently I am using Python module os, when I tried to change the permission of a file, I did not get the expected result. For example, I intended to change the permission to

6条回答
  •  渐次进展
    2020-12-13 02:33

    leading 0 means this is octal constant, not the decimal one. and you need an octal to change file mode.

    permissions are a bit mask, for example, rwxrwx--- is 111111000 in binary, and it's very easy to group bits by 3 to convert to the octal, than calculate the decimal representation.

    0644 (octal) is 0.110.100.100 in binary (i've added dots for readability), or, as you may calculate, 420 in decimal.

提交回复
热议问题