3

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?

Thomas Ahle
  • 30,774
  • 21
  • 92
  • 114
CodeTalk
  • 3,571
  • 16
  • 57
  • 92

1 Answers1

11

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)
Thomas Ahle
  • 30,774
  • 21
  • 92
  • 114
furkle
  • 5,019
  • 1
  • 15
  • 24