fluentfs.filelike.text_file.TextFile

class fluentfs.filelike.text_file.TextFile(path: str, encoding: str = 'utf-8', raise_on_decode_error: bool = True)

Bases: File

__init__(path: str, encoding: str = 'utf-8', raise_on_decode_error: bool = True) None

Initialize a new TextFile from a path.

Parameters:
  • path – The path.

  • raise_on_decode_error – Whether to raise an exception in case of a decoding error. If this is False and the file cannot be read in the given encoding, the content of this file will be returned as an empty string. This is useful if you are iterating over a directory where some files are in a different encoding, and you want to simply ignore these files.

  • encoding – The encoding. This is assumed to be UTF-8 by default.

Methods

__init__(path[, encoding, raise_on_decode_error])

Initialize a new TextFile from a path.

t([encoding, raise_on_decode_error])

Get a TextFile object for this file.

text_file([encoding, raise_on_decode_error])

Get a TextFile object for this file.

Attributes

access_time

The last access time of this file.

atime

The last access time of this file.

byte_count

The number of bytes of this file.

bytes

The content of the file.

cc

The number of characters of this file.

char_count

The number of characters of this file.

content

The content of this file.

dir

The directory containing this file.

empty_line_count

empty_lines

ext

The extension of this file.

extension

The extension of this file.

lc

The number of lines of this file.

line_count

The number of lines of this file.

line_lens

The lengths of the lines of this file.

lines

The lines of this file.

max_line_len

The maximum line length of this file.

mod_time

The last modification time of this file.

mtime

The last modification time of this file.

name

non_empty_line_count

non_empty_lines

path

The maximally expanded path of the file-like object.

relative_path

relpath

size

The size of this file.

wc

The number of words of this file.

word_count

The number of words of this file.

words

The words of this file.

property access_time: datetime

The last access time of this file.

Returns:

A datetime object representing the last access time.

property atime: datetime

The last access time of this file.

Returns:

A datetime object representing the last access time.

property byte_count: int

The number of bytes of this file.

This is similar to wc -c $FILENAME.

Returns:

The number of bytes.

property bytes: bytes

The content of the file.

Returns:

The content bytes.

property cc: int

The number of characters of this file.

This is similar to wc -m $FILENAME.

Returns:

The number of characters.

property char_count: int

The number of characters of this file.

This is similar to wc -m $FILENAME.

Returns:

The number of characters.

property content: str

The content of this file.

Returns:

The content.

property dir: Dir

The directory containing this file.

Returns:

A Dir object representing the directory.

property ext: str

The extension of this file.

Returns:

The extension. If the file has no extension, an empty string will be returned. Otherwise, the extension without the preceding dot will be returned (e.g. “txt”, not “.txt”).

property extension: str

The extension of this file.

Returns:

The extension. If the file has no extension, an empty string will be returned. Otherwise, the extension without the preceding dot will be returned (e.g. “txt”, not “.txt”).

property lc: int

The number of lines of this file.

This is similar to wc -l $FILENAME.

Returns:

The number of lines.

property line_count: int

The number of lines of this file.

This is similar to wc -l $FILENAME.

Returns:

The number of lines.

property line_lens: FunctionalIterator[int]

The lengths of the lines of this file.

Returns:

A functional iterator containing the line lengths of this file.

property lines: FunctionalIterator[str]

The lines of this file.

Returns:

A functional iterator containing the lines of this file.

property max_line_len: int

The maximum line length of this file.

This is similar to wc -L $FILENAME.

Returns:

The maximum line length.

property mod_time: datetime

The last modification time of this file.

Returns:

A datetime object representing the last modification time.

property mtime: datetime

The last modification time of this file.

Returns:

A datetime object representing the last modification time.

property path: str

The maximally expanded path of the file-like object.

property size: FileSize

The size of this file.

Returns:

A FileSize object representing the size of this file (in bytes).

t(encoding: str = 'utf-8', raise_on_decode_error: bool = True) TextFile

Get a TextFile object for this file.

Note that you are responsible to ensure that the underlying file is a valid text file (since this is very expensive to ensure automatically). This function will always succeed, even if the underlying file is not a valid text file. However, when calling paths on the resulting TextFile object, errors will occur.

Parameters:

encoding – The encoding to use.

Returns:

The obtained TextFile object.

text_file(encoding: str = 'utf-8', raise_on_decode_error: bool = True) TextFile

Get a TextFile object for this file.

Note that you are responsible to ensure that the underlying file is a valid text file (since this is very expensive to ensure automatically). This function will always succeed, even if the underlying file is not a valid text file. However, when calling paths on the resulting TextFile object, errors will occur.

Parameters:

encoding – The encoding to use.

Returns:

The obtained TextFile object.

property wc: int

The number of words of this file.

This is similar to wc -w $FILENAME.

Returns:

The number of words.

property word_count: int

The number of words of this file.

This is similar to wc -w $FILENAME.

Returns:

The number of words.

property words: FunctionalIterator[str]

The words of this file.

It is assumed that words are separated by whitespace.

Returns:

A functional iterator containing the words of this file.