| by Arround The Web | No comments

Python File seekable() Method

In Python, while dealing with file data we need to change the position of the file to a particular position using the “seek()” method. But we don’t apply the seek() method if the file is not seekable. To check whether the file is seekable the “file.seekable()” method is used in Python.

This article offers you an in-depth overview of the “file.seekable()” method in Python using below contents:

 

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

In Python, the “file.seekable()” method is utilized to check/verify whether the file stream is seekable. The file is seekable if it allows access to the file stream similar to the seek()” method.

Syntax

file.seekable()

 

Parameters

No parameters. (Here “file” refers to the file object)

Return Value

The “file.seekable()” method retrieves “True” if the particular file is seekable and “False” otherwise.

Example 1: Checking if the File is Seekable Using the “file.seekable()” Method

This example code determines whether the file is seekable utilizing the “file.seekable()” method. First, the file “new.txt” is opened with read mode, and verify whether the file is seekable or not using the Python file “seekable()” method:

file = open("new.txt", "r")
print(file.seekable())

 

The below output verifies that the file is seekable:

Example 2: Checking if the FIle is Seekable by Opening in Various Modes

We can also open the file in different modes to check whether the file is seekable or not. For example, in the below code the file is opened in “a” append mode, and the “file.write()” method is used to write the file. After writing to the file you can use the “file.seekable()” method to check/determine whether the particular file is seekable:

file = open("new.txt", "a")
file.write("Welcome to Linuxhint Guide.")
print(file.seekable())
file.close()

 

According to the following, the file is seekable:

Example 3: Checking if the FIle is Seekable After Closing the File Using “file.seekable()” Method

We can not apply the “file.seekable()” method on the file after closing the file. For instance, consider the below code as an example:

file = open("new.txt", "a")
file.write("Welcome to Linuxhint Guide.")
file.close()
print(file.seekable())

 

The below snippet shows an error:

Difference Between “file.seekable()” Method and “file.seek()” Method

In Python, the “file.seekable()” and “file.seek()” methods are used in file handling with different purposes. For instance, the “seek()” method is utilized to change/modify the position of the file handling to a specific position. Take the below code:

file = open("new.txt", "r")
print(file.seek(11))
print(file.readline())
file.close()

 

After executing the above code, you will get the below output:

On the other hand, the “seekable()” method is used to determine whether the file stream is seekable or not by retrieving the boolean value. Take the following/below code example:

file = open("new.txt", "r")
print(file.seekable())

 

The above code shows the below output:

Conclusion

The Python “file.seekable()” method retrieves the Boolean value to determine whether the specified file stream is seekable or not. The “file.seek()” method modifies the position of the file and reads or writes data to the file. This write-up delivered detailed information on file.seekable() method via several examples.

Share Button

Source: linuxhint.com

Leave a Reply