Python File writable() Method – Tutorial with Examples

Python File writable() Method

The writeable() method in Python is a built-in method used to check if a file is writable or not. It returns True if the file is writable, False otherwise.

Syntax

file.writable()

Return Value

The writeable() method returns a boolean value indicating whether the file is writable or not.

Examples

Example 1: Check if a file is writable

In this example, we will use the writeable() method to check if a file is writable or not:

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

In this example, we opened a file “example.txt” in read mode and used the writeable() method to check if the file is writable. Since we opened the file in read mode, it is not writable and the method returns False.

Output:

False

Example 2: Check if a file is writable after opening in write mode

In this example, we will use the writeable() method to check if a file is writable after opening it in write mode:

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

In this example, we opened a file “example.txt” in write mode and used the writeable() method to check if the file is writable. Since we opened the file in write mode, it is writable and the method returns True.

Output:

True

Example 3: Check if a file is writable after changing its permission

In this example, we will use the writeable() method to check if a file is writable after changing its permission:

import os
# create a file with read permission only
os.system("touch example.txt")
os.system("chmod 400 example.txt")

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

# change the permission to read and write
os.system("chmod 600 example.txt")

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

# remove the file
os.system("rm example.txt")

In this example, we created a file “example.txt” with read permission only using the os.system() method and changed its permission to read and write using the same method. We then used the writeable() method to check if the file is writable before and after changing its permission. Since we opened the file in read mode before changing its permission, it is not writable and the method returns False. After changing its permission, it is writable and the method returns True.

Output:

False
True

Use Cases

The writeable() method can be useful in situations where you need to check if a file is writable before attempting to write to it. This can help prevent errors and ensure that your code runs smoothly.

Conclusion

The writeable() method in Python is a simple yet powerful built-in method that allows you to check if a file is writable or not. It returns a boolean value indicating whether the file is writable or not. This method can be particularly useful in situations where you need to ensure that your code does not try to write to a file that is not writable, which can lead to errors and unexpected behavior. By using the writeable() method, you can prevent these issues and ensure that your code runs smoothly.

Leave a Reply

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