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

前端 未结 7 2278
我寻月下人不归
我寻月下人不归 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:19

    You can also do this

    >>> import os
    >>> st = os.stat("hello.txt")
    

    Current listing of file

    $ ls -l hello.txt
    -rw-r--r--  1 morrison  staff  17 Jan 13  2014 hello.txt
    

    Now do this.

    >>> os.chmod("hello.txt", st.st_mode | 0o111)
    

    and you will see this in the terminal.

    ls -l hello.txt    
    -rwxr-xr-x  1 morrison  staff  17 Jan 13  2014 hello.txt
    

    You can bitwise or with 0o111 to make all executable, 0o222 to make all writable, and 0o444 to make all readable.

    0 讨论(0)
提交回复
热议问题