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)
Author by
CodeTalk
Updated on July 16, 2022Comments
-
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 over 9 yearsthis won't work as you are not joining the paths
-
furkle over 9 years@PadraicCunningham Ah, you're right - editing now.
-
CodeTalk over 9 yearsIs it possible to do: os.remove(d) too?
-
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