Using the following syntactical steps, you can change file permissions in Python using chmod() function within the os module.
1 2 3 4 5 6 7 8 |
import os # Specify the file path file_path = "/path/to/file" # Define the new file permissions new_permissions = <5-character-octal representation of file permission> # Change the file permissions os.chmod(file_path, new_permissions) |
Discussing the Available Permission Sets
A permission set is typically a 3-digit number, but in the octal representation used in Python 3, you have to prefix it with “0o” (Number 0 and letter o).
The first digit after “0o” represents the owner permissions, the second is group permissions, and the last represents other permissions (see Figure below).
The following table lists octal values for setting file permissions. The table also provides file permission sets for convenience: “-” means no permission for the given user, “w” grants write permissions, “r” gives reading permissions, and “x” means the user is given execution permission.
Octal Value | File permission sets | Description |
0 | — | No permission granted |
1 | –x | Execution permission only |
2 | -w- | Write permission only |
3 | -wx | Write and execute permissions |
4 | r- – | Read permission only |
5 | r-x | Read and execute permissions |
6 | rw- | Read and write permissions |
7 | rwx | Read, write, and execute permissions |
Here are examples of octal representations of permissions you can use with the os.chmod() function.
0o400 | Owner read-only |
0o200 | Owner write only |
0o100 | Owner execute only |
0o040 | Group read-only |
0o020 | Group write only |
0o010 | Group execute only |
0o004 | Others read-only |
0o002 | Others write only |
0o001 | Others execute only |
Then you can combine the values to create specific permission sets (see the Table and Figure above for reference). For example,
0o600 | The owner has read and write permissions |
0o644 | Owner read and write, group and others given read-only rights |
0o700 | Give owner full access (read, write, and execute permissions). Group and others are given no access |
0o444 | means all users (owner, groups, and others) are given read permissions to the file |
0o744 | means the owner is given full access, but groups and others are given read rights only |
Examples
Example 1: Changing the permission of a single file
We will save the following code snippet as a file_permission1.py file and execute it from the terminal so that we can check the previous and the new permission sets.
1 2 3 4 5 6 7 8 |
import os # Specify the file path file_path = "/home/kiprono/Desktop/folder1/file1.txt" # Define the new file permissions new_permissions = 0o744 # Change the file permissions os.chmod(file_path, new_permissions) |
Example 2: Updating permissions for all items in a folder.
The following code example provides a way to update permissions for all folder contents.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import os # Specify the directory path dir_path = "/path/to/your/dir" # Define the New folder permissions # 0o700 grants full access To the folder And its content To the # owner And gives no access To the group And others. new_permissions = 0o700 # Change the folder permissions os.chmod(dir_path, new_permissions) # Define a Function To Set file permissions def set_file_permissions(root, filename): # Create the file path file_path = os.path.join(root, filename) # Set the file permissions os.chmod(file_path, new_permissions) # Use os.walk To Set the permissions For all files And directories in the directory For root, dirs, files in os.walk(dir_path): For filename in files: # Update permissions For files set_file_permissions(root, filename) For dirname in dirs: # Update permissions For the folder os.chmod(os.path.join(root, dirname), new_permissions) |
Note that changing the permissions of a folder will also affect the access rights of all files and subfolders within it. Therefore, as needed, you may need to modify the code to set different permissions for files and folders.
Conclusion
This article discussed how to use os.chmod() to set permissions for files and folders using Python. When using this guide, be careful not to provide more permissions than necessary. For example, if you use 0o777, you grant all users full rights to access and modify your files.
In general, granting only the minimum permissions necessary for users to perform their tasks is good practice. This can help reduce the risk of accidental or malicious modification or deletion of files.