fs.mode

Tools for managing mode strings (as used in open() and openbin()).

class fs.mode.Mode(mode)[source]

A mode object provides properties that can be used to interrogate the mode strings used when opening files.

Parameters:mode (str) – A mode string, as used by io.open.
Raises:ValueError – If the mode string is invalid.

Here’s an example of typical use:

>>> mode = Mode('rb')
>>> mode.reading
True
>>> mode.writing
False
>>> mode.binary
True
>>> mode.text
False
appending

Check if a mode permits appending.

binary

Check if a mode specifies binary.

create

Check if the mode would create a file.

exclusive

Check if the mode require exclusive creation.

reading

Check if the mode permits reading.

text

Check if a mode specifies text.

to_platform()[source]

Get a mode string for the current platform.

Currently, this just removes the ‘x’ on PY2 because PY2 doesn’t support exclusive mode.

to_platform_bin()[source]

Get a binary mode string for the current platform.

Currently, this just removes the ‘x’ on PY2 because PY2 doesn’t support exclusive mode.

truncate

Check if a mode would truncate an existing file.

updating

Check if a mode permits updating (reading and writing).

validate(_valid_chars=frozenset([u'a', u'b', u'+', u'r', u't', u'w', u'x']))[source]

Validate the mode string.

Raises:ValueError – if the mode contains invalid chars.
validate_bin()[source]

Validate a mode for opening a binary file.

Raises:ValueError – if the mode contains invalid chars.
writing

Check if a mode permits writing.

fs.mode.check_readable(mode)[source]

Check a mode string allows reading.

Parameters:mode (str) – A mode string, e.g. "rt"
Return type:bool
fs.mode.check_writable(mode)[source]

Check a mode string allows writing.

Parameters:mode (str) – A mode string, e.g. "wt"
Return type:bool
fs.mode.validate_open_mode(mode)[source]

Check mode parameter of open() is valid.

Parameters:mode (str) – Mode parameter.
Raises:ValueError if mode is not valid.
fs.mode.validate_openbin_mode(mode, _valid_chars=frozenset([u'a', u'b', u'+', u'r', u'w', u'x']))[source]

Check mode parameter of openbin() is valid.

Parameters:mode (str) – Mode parameter.
Raises:ValueError if mode is not valid.