Eu aninhei o caminho do diretório cuja permissão estou alterando parte por parte com o seguinte código. Digamos que o caminho seja "/apps/sys/utils/prod/sales/apparel/".
Posso alterar sua permissão de uma só vez em vez de fazer isso parte por parte em um loop para ter o mesmo efeito/resultado?
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")