admin管理员组

文章数量:1355063

I have nested directory path whose permission I am changing part by part with the following code. Lets say the path is "/apps/sys/utils/prod/sales/apparel/".
Can I change its permission in one shot instead of doing it part by part in a loop to have the same effect/result?

     import os
     import stat
     def change_permissions(path, new_mode):
        """Change permissions of the directory to new_mode."""
        if not os.path.islink(path):
            original_mode = os.stat(path).st_mode
            os.chmod(path, new_mode)
            oct_perm = oct(new_mode)
            unix_perm = oct_perm[-3: (len(oct_perm))]
            print(f"Permission of {path} changed to {new_mode} = {unix_perm}")
            return original_mode
        return None
 
     
    target_full_path = "/apps/sys/utils/prod/sales/apparel/shirts.csv"
    # Determine the directory path up to which permissions need to be changed
    target_dir = os.path.dirname(target_full_path)
    print(f"The abstract file dir path upto which perm to be changed {target_dir}")
    # Change permissions for the specific directory needed
    original_permissions = []
    current_dir = "/apps/sys/utils/prod"
    for part in target_dir.split(os.sep)[len(dest_root.split(os.sep)):]:
        current_dir = os.path.join(current_dir, part)
        print(f"\n### part = {part} and current_dir = {current_dir}")
        if not os.path.exists(current_dir):
            os.makedirs(current_dir)
            print(f"Created the current_dir = {current_dir}")
        original_mode = change_permissions(current_dir, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
        original_permissions.append((current_dir, original_mode))
        print (f"Original mode for current_dir ({current_dir}) is {original_mode}\n")

I have nested directory path whose permission I am changing part by part with the following code. Lets say the path is "/apps/sys/utils/prod/sales/apparel/".
Can I change its permission in one shot instead of doing it part by part in a loop to have the same effect/result?

     import os
     import stat
     def change_permissions(path, new_mode):
        """Change permissions of the directory to new_mode."""
        if not os.path.islink(path):
            original_mode = os.stat(path).st_mode
            os.chmod(path, new_mode)
            oct_perm = oct(new_mode)
            unix_perm = oct_perm[-3: (len(oct_perm))]
            print(f"Permission of {path} changed to {new_mode} = {unix_perm}")
            return original_mode
        return None
 
     
    target_full_path = "/apps/sys/utils/prod/sales/apparel/shirts.csv"
    # Determine the directory path up to which permissions need to be changed
    target_dir = os.path.dirname(target_full_path)
    print(f"The abstract file dir path upto which perm to be changed {target_dir}")
    # Change permissions for the specific directory needed
    original_permissions = []
    current_dir = "/apps/sys/utils/prod"
    for part in target_dir.split(os.sep)[len(dest_root.split(os.sep)):]:
        current_dir = os.path.join(current_dir, part)
        print(f"\n### part = {part} and current_dir = {current_dir}")
        if not os.path.exists(current_dir):
            os.makedirs(current_dir)
            print(f"Created the current_dir = {current_dir}")
        original_mode = change_permissions(current_dir, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
        original_permissions.append((current_dir, original_mode))
        print (f"Original mode for current_dir ({current_dir}) is {original_mode}\n")
Share Improve this question edited Mar 30 at 11:15 A.G.Progm.Enthusiast asked Mar 30 at 11:04 A.G.Progm.EnthusiastA.G.Progm.Enthusiast 1,0061 gold badge15 silver badges39 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

If I understand correctly, you want to change the permissions of each individual directory along a tree path, creating each directory beforehand if it doesn't exist.

I don't think that there is a function doing exactly that, and I don't know any other way than changing each directory of the path. That means in a loop.

Except if you accept to :

  1. rely on the OS commands

  2. Change also the directories files permissions

Then you could issue a "chmod -R" shell command on the top-most directory.

What is dest_root"? Shouldn't it be current_dir ?

If you just need to create a directory tree with the given permissions, os.makedirs as a mode parameter.

本文标签: How to change permission of a nested directory in one shot in PythonStack Overflow