fs.mode

Abstract I/O mode container.

Mode strings are used in in open and openbin.

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

An abstraction for I/O modes.

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

Example

>>> mode = Mode('rb')
>>> mode.reading
True
>>> mode.writing
False
>>> mode.binary
True
>>> mode.text
False
__contains__(character: object) bool[source]

Check if a mode contains a given character.

__init__(mode: str) None[source]

Create a new Mode instance.

Parameters:

mode (str) – A mode string, as used by io.open.

Raises:

ValueError – If the mode string is invalid.

property appending

True if the mode permits appending.

Type:

bool

property binary

True if a mode specifies binary.

Type:

bool

property create

True if the mode would create a file.

Type:

bool

property exclusive

True if the mode require exclusive creation.

Type:

bool

property reading

True if the mode permits reading.

Type:

bool

property text

True if a mode specifies text.

Type:

bool

to_platform() str[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() str[source]

Get a binary mode string for the current platform.

This removes the ‘t’ and adds a ‘b’ if needed.

property truncate

True if the mode would truncate an existing file.

Type:

bool

property updating

True if the mode permits both reading and writing.

Type:

bool

validate(_valid_chars: Union[Set[Text], FrozenSet[Text]] = frozenset({'+', 'a', 'b', 'r', 't', 'w', 'x'})) None[source]

Validate the mode string.

Raises:

ValueError – if the mode contains invalid chars.

validate_bin() None[source]

Validate a mode for opening a binary file.

Raises:

ValueError – if the mode contains invalid chars.

property writing

True if the mode permits writing.

Type:

bool

fs.mode.check_readable(mode: str) bool[source]

Check a mode string allows reading.

Parameters:

mode (str) – A mode string, e.g. "rt"

Returns:

True if the mode allows reading.

Return type:

bool

fs.mode.check_writable(mode: str) bool[source]

Check a mode string allows writing.

Parameters:

mode (str) – A mode string, e.g. "wt"

Returns:

True if the mode allows writing.

Return type:

bool

fs.mode.validate_openbin_mode(mode: Text, _valid_chars: Union[Set[Text], FrozenSet[Text]] = frozenset({'+', 'a', 'b', 'r', 'w', 'x'})) None[source]

Check mode parameter of openbin is valid.

Parameters:

mode (str) – Mode parameter.

Raises:

ValueError