python - Python 3 - 如何对给定目录中的所有文件进行 chmod?
问题描述
在 python 3 脚本中,我尝试为目录中的所有 .sh 文件添加执行权限,如下所示:
from os import chmod
chmod('/path_to_dir/dir_prefix_*/bin/*.sh',0o755)
但我收到一个错误:
FileNotFoundError: [Errno 2] No such file or directory:'/path_to_dir/dir_prefix_*/bin/*.sh'
如果我从 bash 运行这个 chmod,它可以正常工作,所以我猜 python 的 chmod 不喜欢*
在路径中使用。
那么在目录中对所有 .sh 文件进行 chmod 的正确方法是什么?
解决方案
只需使用 for 循环。
import os
for file in os.listdir("/mydir"):
if file.endswith(".sh"):
chmod(file, mode)
推荐阅读
- python - Python 脚本只从 url 列表中下载第一个视频
- javascript - 外部 JavaScript 文件将加载,但不执行
- android - 如何在 Android Studio 中查找 gradle 插件的所有可用属性和方法?
- python-3.x - 我是python的初学者,有人可以帮我理解吗?
- php - 使用 codeigniter 从 db 中获取单个数据的更快方法
- python - 通过 Python 中的 Asyncio 进行异步 for 循环
- python - 熊猫列修改面临的问题
- powershell - 通过PowerShell问题发送电子邮件
- java - OpenCSV - keepCarriageReturn 的 CSVReader 问题
- json - 来自前端的休眠映射中的动态属性