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

前端 未结 6 982
轮回少年
轮回少年 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:30

    Found this on a different forum

    If you're wondering why that leading zero is important, it's because permissions are set as an octal integer, and Python automagically treats any integer with a leading zero as octal. So os.chmod("file", 484) (in decimal) would give the same result.

    What you are doing is passing 664 which in octal is 1230

    In your case you would need

    os.chmod("/tmp/test_file", 436)
    

    [Update] Note, for Python 3 you have prefix with 0o (zero oh). E.G, 0o666

提交回复
热议问题