python - 你如何从 python 中做一个简单的 "chmod +x"?

标签 python chmod

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

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

os.chmod 似乎没有像 unix chmod 那样“添加”权限。注释掉最后一行后,文件的文件模式为 -rw-r--r--,没有注释掉的文件模式为 ---x---- --。我怎样才能只添加 u+x 标志,同时保持其余模式不变?

最佳答案

使用 os.stat() 获取当前权限,使用 | 将位组合在一起,并使用 os.chmod() 设置更新的权限。

例子:

import os
import stat

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

关于python - 你如何从 python 中做一个简单的 "chmod +x"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47442348/

相关文章:

Php - Chmod 不工作

PHP chmod 权限被拒绝

python - 为什么我的 Protobuf 消息(在 Python 中)忽略了零值?

python - 构造函数未能接受输入参数

python - 如何使函数在特定时间段内运行?

PHP 和权限

python - 通过 Python 通过 Webform 运行 CSV 列

python - 如何从网站抓取 2 级网页

permissions - rsync部署和文件/目录权限

涉及 PHP chmod( ) :Operation not permitted, safe_mode 弃用?