File Methods
writable()
Return True if the file stream can be written to.
file.writable()
file.writable() checks whether the file was opened with write capabilities (e.g. 'w', 'a', 'r+', 'w+').
Returns
True if writable; otherwise False.
Try it
Run it and change it
import tempfile
with tempfile.NamedTemporaryFile(mode='r', delete=True) as f:
print('Read-only writable:', f.writable())
with tempfile.NamedTemporaryFile(mode='w+', delete=True) as f:
print('Read-write writable:', f.writable())Worth knowing
- Read-only guards: Attempting to call
write()on a read-only stream raises anio.UnsupportedOperationexception.