Python 3 – os.fchmod() Method
The os.fchmod()
method is used to set the permission bits of a file descriptor to the same values as the chmod()
system call. This method is only available on Unix platforms.
Here is the syntax for the fchmod()
method:
os.fchmod(fd, mode)
Parameters
The fchmod()
method requires two parameters:
fd
– this specifies the file descriptor whose permission bits will be set.mode
– this specifies the permission bits that will be set. The mode should be specified as an octal number.
Return Value
The fchmod()
method does not return any value.
Example
In this example, we will use the os.open()
function to create a new file. We will then use the os.fchmod()
method to set the file’s permission bits.
import os
# create a new file
fd = os.open("test.txt", os.O_CREAT | os.O_WRONLY)
# set the file's permission bits to 644
os.fchmod(fd, 0o644)
# close the file descriptor
os.close(fd)
In the above example, we create a new file called “test.txt” using the os.open()
function. We set the file’s permission bits to 644 using the os.fchmod()
method. Finally, we close the file descriptor using the os.close()
method.
Conclusion
In this article, we have learned about the os.fchmod()
method in Python. This method is used to set the permission bits of a file descriptor to the same values as the chmod()
system call. We have also seen an example of how we can use the os.fchmod()
method to set the permission bits of a file.