Python File readable() Method – Tutorial with Examples

Python File readable() Method

The readable() method in Python is a built-in method used to check whether a file can be read or not. It returns a boolean value indicating whether the file can be read or not.

Syntax

file.readable()

Return Value

The readable() method returns a boolean value – True if the file is readable and False otherwise.

Examples

Example 1: Check if file is readable

In this example, we will check if a file is readable using the readable() method:

file = open("example.txt", "w")
file.write("Hello, World!")
file.close()
file = open("example.txt", "r")
print(file.readable())
file.close()

In this example, we opened a file named “example.txt” in write mode, wrote the string “Hello, World!” to the file, and then closed the file. We then opened the file again in read mode, checked if the file is readable using the readable() method, and then closed the file. Finally, we printed the boolean value returned by the readable() method to the console.

Output:

True

Example 2: Check if file is not readable

If a file is opened in write-only mode, it will not be readable. In this example, we will check if a file opened in write-only mode is readable using the readable() method:

file = open("example.txt", "w")
print(file.readable())
file.close()

In this example, we opened a file named “example.txt” in write mode, checked if the file is readable using the readable() method, and then closed the file. Finally, we printed the boolean value returned by the readable() method to the console.

Output:

False

Example 3: Check if file is not present

If the file does not exist, the readable() method will return False. In this example, we will check if a file that does not exist is readable using the readable() method:

file = open("non-existent-file.txt", "r")
print(file.readable())
file.close()

In this example, we attempted to open a file named “non-existent-file.txt” in read mode, checked if the file is readable using the readable() method, and then closed the file. Since the file does not exist, the readable() method will return False. Finally, we printed the boolean value returned by the readable() method to the console.

Output:

False

Use Cases

The readable() method can be useful in situations where we need to check if a file can be read before attempting to read from it. This can be useful for handling exceptions or errors that may occur if we try to read from a file that is not readable. For example, we can use the readable() method to check if a file exists and is readable before attempting to read from it:

try:
    file = open("example.txt", "r")
    if file.readable():
        # read from file
    else:
        print("File is not readable")
    file.close()
except FileNotFoundError:
    print("File not found")
except IOError:
    print("Error reading file")

In this example, we first attempt to open a file named “example.txt” in read mode. We then check if the file is readable using the readable() method. If the file is readable, we can safely read from it. If the file is not readable, we print an error message. If the file does not exist, we catch the FileNotFoundError exception. If there is an error reading the file, we catch the IOError exception.

Conclusion

The readable() method in Python is a useful built-in method for checking if a file is readable. It returns a boolean value indicating whether the file can be read or not. We can use this method to check if a file can be read before attempting to read from it, which can help prevent errors and exceptions.

Leave a Reply

Your email address will not be published. Required fields are marked *