| by Arround The Web | No comments

Python File readable() Method

Files” are used to store various kinds of related data, such as images, text, programs, and others. In Python, different modules and methods are used to handle and manipulate files. Sometimes while reading the file, we need to check/determine whether the file is readable or not. For this purpose, Python provides the inbuilt “file.readable()” method that is used to accomplish this specified task.

This post will explain a thorough tutorial on “file.readable()” Method utilizing multiple examples:

What is the Python “file.readable()” Method?

In Python, the “file.readable()” method is employed to check/determine whether the particular file is readable or not by retrieving the boolean value.

Syntax

Here is the general syntax of the “file.readable()” method:

file.readable()

 

Parameters

None

Return Value

The “file.readable()” method retrieves the “True” Boolean value if the particular file is readable otherwise “False”.

Example 1: Applying the “file.readable()” Method to Determine Whether the File is Readable or Not

This example is used to determine the file readability using the “file.readable()” method. Here is the original file with the data and name that is used in the below code:

According to the below code, the “open()” function is used to open the “example.txt” file in read mode and determines the readability of the file using the “file.readable()” method:

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

 

The “True” value in the output shows that the file is readable:

Example 2: Applying “file.readable()” Method to Check Whether the File is Readable or Not Using Different Mode

Now, we will open the file in a different mode, such as append and write mode, to determine the file readability. First of all, we used the “open()” function to open the file in write mode and then check the file readability using the “file.readable()” method:

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

 

The “False” value signifies that the file opened in write mode cannot be readable:

In the below code, the “open()” function is utilized to open the file in “append” or “a” mode. The “file.readable()” method applies to the file object and retrieves the Boolean value that determines whether or not the file is readable:

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

 

The execution of the above code retrieves the below output:

Example 3: Applying “file.readable()” Method to Check Whether the User Input File is Readable or Not

The “input()” function is used to input the filename from the user, and the “readable()” method is used to check the readability of the user input file. The “if-else” statement is used to retrieve the specified message based on whether or not the file is readable:

file = input("Enter File Name: ")
f1 = open(file, "r")
if f1.readable():
    print("\nInput File is Readable.")
else:
    print("\nInput File is not Readable.")

 

The below snippet verified that the input file could be readable:

Example 4: Handling Error While Checking Whether the File is Readable or Not

The “FileNotFoundError” arises when we read the file and check its readability which does not exist in the system. For example, take the following snippet:

The above snippet shows the file not found error which represents that the file is not present in the system. In order to solve this error, the “try-except” block is employed in the program/code. The below-provided code removes the error by using this technique:

try:
    file = open("sample.txt", "r")
    print(file.readable())
except:
    print('No Such File is Present in the Directory!')

 

The except block executed and retrieves the following message if the file is not present:

Conclusion

The inbuilt “file.readable()” method is used in Python to determine whether the particular file is readable or not by retrieving the Boolean value. The file is not readable when the “open()” function opens the file in “write” or “append” mode. This guide covered the “file.readable()” method using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply