smizzlov
smizzlov

Reputation: 608

Difference between os.chmod, os.fchmod and os.lchmod

I want to change file modes with Python.

The os module has three functions that seem functionally equivalent:

What are the differences between these three versions?

Upvotes: 3

Views: 1046

Answers (3)

suresh Palemoni
suresh Palemoni

Reputation: 1226

All three methods are being used to change the mode of the file.

 - chmod: os.chmod(path, mode, *, dir_fd=None, follow_symlinks=True)

chmod by default accepts the path and numeric mode and changes the file permissions. By default it follows symlinks and file


- fchmod: os.fchmod(fd, mode)

Instead of path you may pass the open file descriptor.


- lchmod: os.lchmod(path, mode)

This works same as chmod() however the argument follow_symlinks=True changes to follow_symlinks=False. By affecting on the symlink file rather than target.

Upvotes: 2

Jacques Gaudin
Jacques Gaudin

Reputation: 16988

chmod is used to change the file permissions of a file specified by path.

fchmod is used to change the file permissions of a file specified by file descriptor.

lchmod is similar to chmod() but does not follow symbolic links.

You can read more in the man page

Upvotes: 4

Emre Sevinç
Emre Sevinç

Reputation: 8521

According to documentation os.fchmod(fd, mode) is equivalent to os.chmod(fd, mode) since Python 3.3 (fd: file descriptor, a non-negative integer, used as an abstract indicator (handle) to access a file or other input/output resource, such as a pipe or network socket).

I suggest you read the BSD man page at https://www.freebsd.org/cgi/man.cgi?query=lchmod to dive into details. E.g. the main difference between lchmod and chmod: lchmod does not follow symbolic links.

Upvotes: 1

Related Questions