readable()
Returns boolean value True or False. Returns True if file is readable , otherwise False .
fob=open('data1.txt','r')
print(fob.readable()) # True
fob=open('data1.txt','w')
print(fob.readable()) # False
fob=open('data1.txt','a')
print(fob.readable()) # False
fob=open('data1.txt','+a')
print(fob.readable()) # True
When trying to open a file that doesn't exist, Python will raise an error. We can handle this with a try-except block:
try:
fob = open('non_existent.txt', 'r')
print(fob.readable())
except FileNotFoundError:
print("File not found.")
This can be useful in scenarios where a program needs to ensure read access before processing the file:
fob = open('data.txt', 'r')
if fob.readable():
print("File is readable, proceeding with operations.")
else:
print("File is not readable.")
writable()