In this tutorial we will learn about Python chmod method. This method is used for changing permissions for files and directories.
Python chmod Method
In Python the method chmod() changes the mode of path to the passed numeric mode. The mode can be octal values for permissions.
Syntax
Here is the syntax for Python chmod method:
os.chmod(path, mode)
Parameters
This method takes only two parameters.
- path – This is the path of which you need to change the permission
- mode – This parameter may take any octal value which is used for file permission (ex: 0775).
Example
Here is a simple example for Python chmod method in which we will change the permission of a file. Lets assume that we have a file named myfile.txt
in /home/mydir/
. And we will change its permission to 0775
.
#!/usr/bin/python import os, sys # Assuming that /home/mydir/myfile.txt exists. os.chmod("/home/mydir/myfile.txt", 0775) # Printing a message for the output print "You have changed your file's mode successfully"
After running this script you will see that your file’s mode has been changed.
The post Python chmod Method: Python Tutorial appeared first on JS Tricks.