How to Change File Permissions in Python?

5/5 - (10 votes)

Problem Formulation and Solution Overview

When a new file or folder is created, it comes with its own set of default permissions. From time to time, you will need to adjust these permissions.

To follow this article, download the finxter.csv file and move this file to the current working directory.

What are File Permissions?

File Permissions are file settings that, when set, can determine who can access specific files and what an individual or group can do with those files, for example, modify or delete them.

For example, on Windows, if you right-mouse click over the finxter.csv file downloaded earlier, the file permissions will appear similar to below.

As you can see, there are several permissions available. These permissions can be adjusted in a Windows environment, another environment, or Python.


💬 Question: How would we write code to change file permissions?

We can accomplish this task by one of the following options:


Method 1: Use stat() and chmod()

This example imports two (2) libraries, stat and os and calls stat()and chmod() to change the permissions to Read Only for the file owner.

import stat
import os

os.chmod('finxter.csv', stat.S_IRUSR)

The above imports two (2) libraries to allow the code in this snippet to run error-free.

Next, chmod() is called and passed two (2) arguments, the filename (path argument) and stat.S_IRUS (mode argument). This last one changes the file permissions to Read by Owner.

For this example, to confirm the file is now Read Only by Owner, open finxter.csv, make a change and try to save the file.

If the code worked correctly, a pop-up similar to below will display.

💡Note: There is quite a list of file permissions available. Please click here to delve further into this topic.

To set two (2) or more file permissions, use the pipe (|) separator character, as shown below.

import stat
import os

os.chmod('finxter.csv', stat.S_IWRITE | stat.S_IEXEC)

The above code, changes permissions based on the parameters passed. stat.S_IWRITE (owner has write permission) and stat.S_IEXEC (owner has execute permission).


Method 2: Use a one-liner and chmod() at the command prompt

This example is run at the command prompt to change file permissions using one line of Python code.

$ python -c "import os; os.chmod('finxter.csv', 0o777)"

The above code imports the os library, then changes the file permissions to 777 using the Octal format (0o777).

Changing a file permissions to 777 gives all users Read, Write, and Execute access, otherwise known as Full Access.

If you open finxters.csv, make a change and try to save, you should no longer received a read-only error.

To set different permissions, run the code below to set file permissions to 755. These permissions allow the owner to Read, Write, Execute, and Group/Others can Read and Execute.

$ python -c "import os; os.chmod('finxter.csv', 0o755)"

Method 3: Use oschmod and numbers

This example uses a new security utility oschmod to change file system object permissions on Windows, Linux and MacOS systems at the command prompt. Awesome!

Before running any code, the oschmod library must be installed. To do this, navigate to an IDE terminal and enter the following at the command prompt.

pip install oschmod

After a successful installation, run the code below to change file permissions to 655. This permission changes, so now the file owner can read and write. Everyone else can read, and no one can execute.

$ oschmod 644 finxter.csv


Method 4: Use oschmod and alpha

This example shows three (3) different ways to set file permissions to

$ oschmod a+rwx,g-w,o-x 'finxter.csv'

The above example is run at the command prompt and grants all users permissions. Then, removes the execute and write permissions from the group and execute permissions from all others.

Instead of using numeric values, the same can be done using an alpha character and a plus (+) sign to add permissions and a minus (-) sign to remove permissions.


Summary

This article has provided four (4) ways to change file permissions to select the best fit for your coding requirements.

Good Luck & Happy Coding!


Programming Humor

💡 Programming is 10% science, 20% ingenuity, and 70% getting the ingenuity to work with the science.

~~~

  • Question: Why do Java programmers wear glasses?
  • Answer: Because they cannot C# …!

Feel free to check out our blog article with more coding jokes. 😉