Recursively set all files and folders to 777 from this list

13,750

You can iterate through the directories you'd like to modify, then use os.walk to iterate over all of the directories and files in each of these directories, like so:

for your_dir in dirs_to_modify:
    for root, dirs, files in os.walk(your_dir):
        for d in dirs:
            os.chmod(os.path.join(root, d), 0o777)
        for f in files:
            os.chmod(os.path.join(root, f), 0o777)
Share:
13,750
CodeTalk
Author by

CodeTalk

Updated on July 16, 2022

Comments

  • CodeTalk
    CodeTalk almost 2 years

    I've tried endlessly searching for a solution to this, but couldn't seem to find out.

    I have a list of paths:

    dirToModify = ['includes/','misc/','modules/','scripts/','themes/']
    

    I want to loop through each list-item (a dir in this case) and set all files and dirs to 777

    I've done this for files like:

    for file in fileToModify:
        os.chmod(file, 0o777)
    

    But can't seem to figure out a good way to do this recursively to folders.

    Can anyone help?

  • Padraic Cunningham
    Padraic Cunningham over 9 years
    this won't work as you are not joining the paths
  • furkle
    furkle over 9 years
    @PadraicCunningham Ah, you're right - editing now.
  • CodeTalk
    CodeTalk over 9 years
    Is it possible to do: os.remove(d) too?
  • furkle
    furkle over 9 years
    @CodeTalk To remove a directory? You'd probably want to use shutil.rmtree: docs.python.org/2/library/shutil.html#shutil.rmtree