简体   繁体   中英

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

In a python 3 script I'm trying to add execution permissions to all.sh files in a directory, as follows:

from os import chmod
chmod('/path_to_dir/dir_prefix_*/bin/*.sh',0o755)

But I'm getting an error:

FileNotFoundError: [Errno 2] No such file or directory:'/path_to_dir/dir_prefix_*/bin/*.sh'

If I run this chmod from bash, it works ok, so I guess python's chmod does not like the use of * in the path.

What would be the correct way to chmod all.sh files in a directory then?

Just use a for loop.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM