From Python Code, you can access files either for reading or to write or even new files can be created using Python.
There are multiple ways which can be used for accessing files into Python code, but before doing that you need to check whether File exists or not. Moreover you want to write to some file, but some permissions for that file are set such that you cannot write to it, then firstly you need to change File Permissions.
In this article, I will be discussing How to change these File Permissions using Python? But before that let’s see How does Python sets File Permissions?
How does Python sets File Permissions?
For accessing files from Python os.access() function can be used which takes in path, mode, *, dir_fd=None, effective_ids=False, follow_symlinks=True as parameters. Here mode parameter is specifically for setting File Permisions, if its R_OK then it means file can be read.
Different File Permissions in Python
|Existence of File||F_OK|
|File is readable||R_OK|
|Writability of File||W_OK|
|Executability of Path||X_OK|
How to check permission of File using Python?
For checking file permissions os.access(path, mode) function can be used.
- mode = os.F_OK => Checks for File Existence
- mode = os.R_OK => Checks for Read Access
- mode = os.W_OK => Checks for Write Access
- mode = os.X_OK => Checks for Execution Access
Below are some code examples showing How to check File Permission in Python.
import os print(os.access('my_file', os.R_OK)) # Check for read access print(os.access('my_file', os.W_OK)) # Check for write access print(os.access('my_file', os.X_OK)) # Check for execution access print(os.access('my_file', os.F_OK)) # Check for existance of file
Let’s now see how to change file permission, if it need to be changed?
Changing File Permission using Python
File permissions can be changed using os.chmod(path, mode) function. Like setting mode to stat.S_IREAD will set file permission such that file would be only readable by file owner itself.
Common mode values for os.chmod(path, mode) function
|stat.S_IREAD||Sets file permission such that it will be only readable by file owner|
|stat.S_IROTH||Sets file permission such that it will be only readable by anyone|
|stat.S_IWrite||Sets file permission such that it can only be written by owner|
|stat.S_IXUSR||Sets file permission such that it can only be executed by owner|
Python Code Examples – File Permission Change
# Python program to explain os.chmod() method # importing necessary libraries import os, sys, stat # Set given file read by the owner. os.chmod("somefile", stat.S_IREAD) print("File can be read only by owner.") # Set given file read by others. os.chmod("somefile1", stat.S_IROTH) print("File access changed, can be read by others now.")
# Python program to explain os.chmod() method # importing necessary libraries import os, sys, stat # Set given file written by the owner. os.chmod("somefile", stat.S_IWRITE) # Set given file executed by the owner. os.chmod("somefile1", stat.S_IXUSR) print("File can be written and executed only by owner.")
- Before reading a file, make sure that you have appropriate permission or before accessing file for reading just add an if statement to check whether file is permitted to be read.
- Before writing to a file, make sure that you have permission for doing so. Also like reading a file, do a permssion check before writing to file using if statement.
- Before accessing a file from File System either for reading/writing make sure to check whether file exists or not.
For learning about how to check whether file exists or not see – 3 Ways to Check if File exists in Python article
Happy Coding 🥳
If you have any question, do comment and I’ll rep