scenario.textfile module¶
Text file management.
- class TextFile¶
Bases:
object
Wrapper for reading and writing text files.
This class doesn’t aim to be a faithful IO class, but rather a wrapper to common file operations.
- __init__(path, mode, encoding=None)¶
- Parameters:
path – File path.
mode – “r” or “w”.
encoding – Explicit file encoding when specified. Will be guessed from the input file when reading otherwise. UTF-8 by default.
- _file¶
Python file instance.
- encoding¶
File encoding.
- _guessencoding()¶
Tries to guess the file encoding from a dedicated comment in the first lines of it.
Stores the result in the
encoding
attribute.
- read(size=-1)¶
Reads a string from the file.
- Parameters:
size – Size to read.
- Returns:
Content string.
- readlines()¶
Reads all lines from a file.
- Returns:
File lines.
- write(text)¶
Writes a string to the file.
- Parameters:
text – String to write.
- Returns:
Number of bytes written. May not equal the string length depending on the encoding.
- close()¶
Closes the file.
- guessencoding(path)¶
Return the encoding guessed from a text file.
- Parameters:
path – Path of the file to guess encoding for.
- Returns:
Encoding guessed from the file. UTF-8 by default.
See also