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

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

    If you're using Python 3.4+, you can use the standard library's convenient pathlib.

    Its Path class has built-in chmod and stat methods.

    from pathlib import Path
    import stat
    
    
    f = Path("/path/to/file.txt")
    f.chmod(f.stat().st_mode | stat.S_IEXEC)
    

提交回复
热议问题