Skip to main content

writable()

The writable() function is a method in Python that is used with file objects to check if the file is open for writing. It returns True if the file is open in write mode and False otherwise.

Parameter Values

This function does not accept any parameters.

Return Values

The writable() method returns a bool indicating if the file stream is writable.

How to Use writable() in Python

Example 1:

The writable() method returns True if the file is open for writing, otherwise it returns False.

with open('myfile.txt', 'w') as file:
    print(file.writable())
Example 2:

Using writable() on a file opened in read mode will return False.

with open('myfile.txt', 'r') as file:
    print(file.writable())
Example 3:

writable() can be used to check if the file is open for writing before writing to it.

with open('myfile.txt', 'a') as file:
    print(file.writable())