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

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

    For tools that generate executable files (e.g. scripts), the following code might be helpful:

    def make_executable(path):
        mode = os.stat(path).st_mode
        mode |= (mode & 0o444) >> 2    # copy R bits to X
        os.chmod(path, mode)
    

    This makes it (more or less) respect the umask that was in effect when the file was created: Executable is only set for those that can read.

    Usage:

    path = 'foo.sh'
    with open(path, 'w') as f:           # umask in effect when file is created
        f.write('#!/bin/sh\n')
        f.write('echo "hello world"\n')
    
    make_executable(path)
    
    0 讨论(0)
  • 2020-11-29 20:03

    Respect umask like chmod +x

    man chmod says that if augo is not given as in:

    chmod +x mypath
    

    then a is used but with umask:

    A combination of the letters ugoa controls which users' access to the file will be changed: the user who owns it (u), other users in the file's group (g), other users not in the file's group (o), or all users (a). If none of these are given, the effect is as if (a) were given, but bits that are set in the umask are not affected.

    Here is a version that simulates that behavior exactly:

    #!/usr/bin/env python3
    
    import os
    import stat
    
    def get_umask():
        umask = os.umask(0)
        os.umask(umask)
        return umask
    
    def chmod_plus_x(path):
        os.chmod(
            path,
            os.stat(path).st_mode |
            (
                (
                    stat.S_IXUSR |
                    stat.S_IXGRP |
                    stat.S_IXOTH
                )
                & ~get_umask()
            )
        )
    
    chmod_plus_x('.gitignore')
    

    See also: How can I get the default file permissions in Python?

    Tested in Ubuntu 16.04, Python 3.5.2.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 20:19

    If you know the permissions you want then the following example may be the way to keep it simple.

    Python 2:

    os.chmod("/somedir/somefile", 0775)
    

    Python 3:

    os.chmod("/somedir/somefile", 0o775)
    

    Compatible with either (octal conversion):

    os.chmod("/somedir/somefile", 509)
    

    reference permissions examples

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