簡體   English   中英

Python 3 - 如何更改給定目錄中的所有文件?

[英]Python 3 - How to chmod all files in a given directory?

在 python 3 腳本中,我嘗試向目錄中的 all.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 不喜歡在路徑中使用*

那么在目錄中 chmod all.sh 文件的正確方法是什么?

只需使用 for 循環。

import os
for file in os.listdir("/mydir"):
    if file.endswith(".sh"):
        chmod(file, mode)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM