Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of "chmod -R u+x $path" in Python?

Tags:

python

bash

What's the equivalent of this bash command in Python?

chmod -R u+x $dir_path

There is os.fchmod but not sure if it applies recursively as the original command does.

Does this do the same thing that I want?

import os
import stat
from pathlib import Path

fd = Path(dir_path).open().fileno()
os.fchmod(fd, stat.S_IXUSR)
like image 292
adhnguyen Avatar asked Oct 20 '25 04:10

adhnguyen


1 Answers

You can use os.chmod but that'll only change permissions of that individual file or directory. To change permissions of a single file incrementally (by adding to the existing permissions), use:

import os
import stat

st = os.stat("path/to/file")
os.chmod("/path/to/file", st.st_mode | stat.S_IXUSR)

Now, to change permissions of files in a directory recursively, you would need to use os.walk to traverse through all sub directories and their files:

import os
import stat

for root, dirs, files in os.walk("path/to/directory"):
    for name in dirs:
        path = os.path.join(root, name)
        st = os.stat(path)
        os.chmod(path, st.st_mode | stat.S_IXUSR)
    for name in files:
        path = os.path.join(root, name)
        st = os.stat(path)
        os.chmod(path, st.st_mode | stat.S_IXUSR)

The above will both traverse subdirectories and will incrementally modify permissions only to add user execution permissions.

There are other alternatives, one fun and obvious one is:

os.system("chmod -R u+x /path/to/file")

For future reference, here are two related questions:

  • How to use Python's os.chmod recursively
  • How to do chmod +x using Python's os.chmod
like image 194
Rudy Matela Avatar answered Oct 22 '25 20:10

Rudy Matela