如何从python中执行简单的“chmod +x”?

时间:2022-10-30 13:06:45

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

我想从一个可执行的python脚本中创建一个文件。

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

it appears os.chmod doesn't 'add' permissions the way unix chmod does. With the last line commented out, the file has the filemode -rw-r--r--, with it not commented out, the file mode is ---x------. How can I just add the u+x flag while keeping the rest of the modes intact?

看来操作系统。chmod不像unix chmod那样“添加”权限。最后一行注释掉了,文件有filemode -rw-r- r-,没有注释掉,文件模式是------ -----。我怎么才能在保持其他模式不变的情况下添加u+x标志呢?

4 个解决方案

#1


134  

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

使用os.stat()获取当前权限,使用|或二进制文件,并使用os.chmod()来设置更新的权限。

Example:

例子:

import os
import stat

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

#2


13  

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.

这使得它(或多或少)尊重在创建文件时所产生的umask:可执行文件只针对那些可以读取的文件。

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)

#3


3  

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

如果您知道您想要的权限,那么下面的示例可能是保持它简单的方法。

Python 2:

Python 2:

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

Python 3:

Python 3:

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

Compatible with either (octal conversion):

与任一(八进制转换)兼容:

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

reference permissions examples

参考权限的例子

#4


2  

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.

您可以将所有可执行文件(0o222)按位或与0o111进行处理,以使所有可写操作,以及0o444使所有可读。

#1


134  

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

使用os.stat()获取当前权限,使用|或二进制文件,并使用os.chmod()来设置更新的权限。

Example:

例子:

import os
import stat

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

#2


13  

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.

这使得它(或多或少)尊重在创建文件时所产生的umask:可执行文件只针对那些可以读取的文件。

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)

#3


3  

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

如果您知道您想要的权限,那么下面的示例可能是保持它简单的方法。

Python 2:

Python 2:

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

Python 3:

Python 3:

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

Compatible with either (octal conversion):

与任一(八进制转换)兼容:

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

reference permissions examples

参考权限的例子

#4


2  

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.

您可以将所有可执行文件(0o222)按位或与0o111进行处理,以使所有可写操作,以及0o444使所有可读。