Python

Python File writable() Method

Python provides a built-in file-handling system that makes it easy to work/deal with files. We can open the file in various formats and modes as well as perform certain operations. While reading, appending, or writing files in Python, sometimes we need to check whether the file is writable or not. We can also check this to avoid errors or exceptions. For that purpose, the “file.writable()” method is utilized in Python.

This guide will deliver a comprehensive tutorial on the Python “file.writable()” method via the below contents:

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

In Python, the “file.writable()” method is utilized to check/determine whether the file is writable. If the file is writable, then we can use “a” append or “w” write mode to access the file.

Syntax

file.writable()

 

Parameters

No parameters.

Return Value

The “file.writable()” method retrieves the boolean value. The “True” represents that the file is writable and “False” represents that the file is not writable.

Example 1: Determining if the File is Writable While Opening the File in “a” Append or “w” Write Mode

In the below code, the “open()” function opens the particular “new.txt” file in read/reading mode. Next the “file.writable()” method checks whether the input file is writable or not:

file = open("new.txt", "a")
print(file.writable())

 

The below boolean value specifies that the input file is writable:

We can also open the file in write “w” write mode using the “open()” function and check whether it is writable or not utilizing the “file.writable()” method:

file = open("new.txt", "w")
print(file.writable())

 

The following snippet verified that the particular file is writable:

Example 2: Determining if the File is Writable While Opening the File in “r” Read Mode

In this code, we open the file in read “r” mode and check whether it is writable or not. Take the following code as an example:

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

 

The below snippet verified that the file is not writable:

Example 3: Determining if the File is Writable and Write String to the File

In the following code, first, we need to check/determine whether the file is writable. After checking if the file is writable, the “file.write()” method is used to write the specified string to the file:

file = open("new.txt", "w")
print(file.writable())
file.write('Welcome to Linuxhint!')
file.close()

 

First look at the console output that verifies that the file is writable:

Here is the text file that shows the content that is included or inserted using the file.write() method:

Example 4: Handling Error While Checking if the file is Writable

This example will show you how we can handle the error while checking if the file is writable or not. According to the following code, the file we need to open in read mode is not present in the system. The “try-except” method is utilized together with the “if-else” statement to check/determine whether the file is writable:

try:
    file = open("new1.txt", "r")
    if file.writable():
        print("The Given File is Writable.")
    else:
        print("The Given File is not Writable.")
except FileNotFoundError:
    print("File not found!")

 

The below output signifies that the file is not present in the directory:

Now, we can open the file in “append” mode that is present in the current working directory. Here, the if-else statement checks whether the file is writable or not and retrieves the boolean value:

try:
    file = open("new.txt", "a")
    if file.writable():
        print("The Given File is Writable.")
    else:
        print("The Given File is not Writable.")
except FileNotFoundError:
    print("Given File not found!")

 

The below snippet verifies that the file is writable:

Conclusion

The “file.writable()” method in Python is utilized to determine whether the input file is writable or not according to the boolean value. The boolean “True” signifies that the file is writable and “False” signifies that the file is not writable. This tutorial delivered a comprehensive guide on Python’s “file.writable()” method utilizing several examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.