Open In App

Check If File is Readable in Python

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We are given a file and we have to check whether the file is readable in Python or not. In this article, we will see how we can check if a file is readable or not by using different approaches in Python.

How to Check if a File is Readable in Python

Below, are the methods of How to Check If a File Is Readable in Python:

Check if a File is Readable in Python Using os Module

In this example, below Python code below uses the Python OS Module to check if the file “file.txt” is readable. If the file is readable (`os.R_OK`), it prints “File is readable”; otherwise, it prints “File is not readable.” The `os.access` function helps ensure proper file accessibility.

Python3




import os
 
file_path = "file.txt"
 
if os.access(file_path, os.R_OK):
    print("File is readable")
else:
    print("File is not readable")


Output:

File is readable

Check if a File is Readable in Python Using Try Except Block

In this example, below Python code attempts to open and perform operations on the file “file.txt”. If successful, it prints “The File is readable”. If an IOError occurs (e.g., file not found or permission denied), it prints “Error: File is not readable”. This try-except block ensures proper handling of potential file accessibility issues.

Python3




# file path
file_path = "file.txt"
 
try:
    # We try to open the file and perform operations on it.
    with open(file_path) as file:
        # print a message indicating that the file is readable.
        print("The File is readable")
# If an IOError occurs (e.g., file not found or permission denied),
except IOError:
    # Print an error message indicating that the file is not readable.
    print("Error: File is not readable")


Output:

File is readable

Check if a File is Readable in Python Using os.path.isfile() Module

In this example, below Python code checks if the file “file.txt” exists and is readable using `os.path.isfile` and `os.access`. If both conditions are met, it prints “File is readable.” Otherwise, if the file does not exist or is not readable, it prints “File is not readable.” This ensures a robust check for file accessibility before attempting further operations.

Python3




# path of the file
file_path = "file.txt"
 
# Check if the file exists and is readable.
if os.path.isfile(file_path) and os.access(file_path, os.R_OK):
    # If the file exists and is readable, print a message that the file is readable.
    print("File is readable")
else:
    # If the file does not exist or is not readable, print a message that the file is not readable.
    print("File is not readable")


Output:

File is readable 

Conclusion

In conclusion, ensuring a file is readable is essential when working with files in Python. Checking readability before attempting to read enhances code reliability and overall performance. By employing the discussed methods, Python developers gain the ability to make informed choices regarding file accessibility. This, in turn, minimizes the risk of errors during runtime, fostering the development of stronger, more resilient applications.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads