Changing File Permission in Python

Using the following syntactical steps, you can change file permissions in Python using chmod() function within the os module.

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.

Example 2: Updating permissions for all items in a folder.

The following code example provides a way to update permissions for all folder contents.

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.