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

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

    Use os.stat() to get the current permissions, use | to or the bits together, and use os.chmod() to set the updated permissions.

    Example:

    import os
    import stat
    
    st = os.stat('somefile')
    os.chmod('somefile', st.st_mode | stat.S_IEXEC)
    

提交回复
热议问题