你如何在 python 中做一个简单的“chmod +x”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12791997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:53:32  来源:igfitidea点击:

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

pythonchmod

提问by priestc

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.chmoddoesn't 'add' permissions the way unix chmoddoes. 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+xflag while keeping the rest of the modes intact?

它似乎os.chmod没有像 unixchmod那样“添加”权限。注释掉最后一行后,文件具有 filemode -rw-r--r--,没有注释掉,文件模式为---x------. 如何u+x在保持其余模式不变的情况下只添加标志?

采纳答案by Ignacio Vazquez-Abrams

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)

回答by Jonathon Reinhart

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 umaskthat 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)

回答by ncmathsadist

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.

您可以按位或使用 0o111 使所有可执行文件,0o222 使所有可写,和 0o444 使所有可读。

回答by zerocog

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

如果您知道所需的权限,那么以下示例可能是保持简单的方法。

Python 2:

蟒蛇2:

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

Python 3:

蟒蛇3:

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

Compatible with either (octal conversion):

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

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

reference permissions examples

参考权限示例

回答by funkid

In python3:

在python3中:

import os
os.chmod("somefile", 0o664)

Remember to add the 0oprefix 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.

请记住添加0o前缀,因为权限设置为八进制整数,Python 会自动将任何带有前导零的整数视为八进制。否则,您os.chmod("somefile", 1230)确实正在通过,这是664.

回答by cs01

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

如果您使用的是 Python 3.4+,则可以使用标准库中方便的pathlib

Its Pathclass has built-in chmodand statmethods.

它的Path类具有内置的chmodstat方法。

from pathlib import Path


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