How do you do a simple “chmod +x” from within python?

前端 未结 7 2306
我寻月下人不归
我寻月下人不归 2020-11-29 19:58

I want to create a file from within a python script that is executable.

import os
import stat
os.chmod(\'somefile\', stat.S_IEXEC)

it appea

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 20:13

    In python3:

    import os
    os.chmod("somefile", 0o664)
    

    Remember to add the 0o prefix since permissions are set as an octal integer, and Python automatically treats any integer with a leading zero as octal. Otherwise, you are passing os.chmod("somefile", 1230) indeed, which is octal of 664.

提交回复
热议问题