0

I want to remove all write permissions from a directory and all it's sub-directories and files with python

I want something like this:

os.chmod(path, "a-w", recursive=True)

In bash it would be this:

chmod -R a-w $path
wotanii
  • 2,470
  • 20
  • 38

2 Answers2

2

One way is to use subprocess module. Your code will look like that:

import subprocess

subprocess.call(['chmod', '-R', 'a-w', path])

For os-based solution see this answer.

Seer.The
  • 475
  • 5
  • 15
1
import subprocess

try:
    subprocess.check_call(["chmod -R a-w {path}".format(path=path)], shell=True) # Or  subprocess.check_call(["chmod", "-R", "a-w", path])
except subprocess.CalledProcessError:
    pass # handle errors in the called executable
except OSError:
    pass # executable not found
MaNKuR
  • 2,578
  • 1
  • 19
  • 31