File Methods
readable()
Return True if the file stream can be read from.
file.readable()
file.readable() checks whether the file object was opened in a readable mode (such as 'r', 'r+', 'w+', 'a+').
Returns
True if readable; otherwise False.
Try it
Run it and change it
import tempfile
with tempfile.NamedTemporaryFile(mode='w', delete=True) as f:
print('Write-only readable:', f.readable())
with tempfile.NamedTemporaryFile(mode='r', delete=True) as f:
print('Read-only readable:', f.readable())Worth knowing
- Mode validation: Attempting to call
read()on a non-readable stream raises anio.UnsupportedOperationerror.