API Reference

Here you can see the docs for all the different methods, classes, etc.

AbstractFile

class filehandlers.AbstractFile(name)

A file in instance form.

Parameters:name (str) – The file name
__init__(name)

Create the class.

Parameters:name (str) – The file name
Returns:None
Return type:NoneType
__str__()

Override str().

Returns:the name
Return type:str
change_file_name(n)

Changes the file name.

Important

This doesn’t change the file’s actual name, it changes the name of the file focused on by this AbstractFile instance. We suggest you don’t use this because you can just create different AbstractFile instances for different files.

Returns:None
Return type:NoneType
Parameters:n (str) – the new name for the file
wrap(doreturn=True)

Wrap file in TextIOWrapper.

Parameters:doreturn (bool) – Just keep this True (or don’t pass the keyword argument).
Returns:The wrapper
Return type:io.TextIOWrapper
Raises:PermissionError – If you don’t have needed permission to access the file
touch()

Create the file if it doesn’t already exist.

Important

This is the only method that actually changes/interacts with the file inside the AbstractFile class (other than wrap() and exists()).

In case you are wondering, the name for this function comes from the Unix command (touch), which creates a new file with the name as a parameter.

Returns:None
Return type:NoneType
Raises:PermissionError – If you don’t have needed permission to access the file
exists(touch_if_false=False)

Get if this file exists or not (boolean value).

Returns:If the focused file exists
Return type:bool:
Parameters:touch_if_false (bool) – If the file should be created if it doesn’t exist. Defaults to False.
Throws PermissionError:
 If you don’t have the required permissions to access the file.

FileManipulator

class filehandlers.FileManipulator(abstract_file)

Class used for managing it’s assigned file.

Parameters:abstract_file (AbstractFile) – the file to manage
__init__(abstract_file)

Create class instance.

Parameters:abstract_file (AbstractFile) – the AbstractFile instance
Returns:None
Return type:NoneType
Raises:TypeError
get_file()

Get the AbstractFile instance.

Returns:the AbstractFile instance
Return type:AbstractFile
get_file_name()

Get the file’s name.

Returns:The file’s name
Return type:str
refresh(slim=False)

Update the cache.

Parameters:slim (bool) – (Optional) - if empty lines should be removed - defaults to True.
Returns:None
Return type:NoneType
Raises:PermissionError – If you don’t have needed permission to access the file
get_cache()

Get the cache.

The cache will be a list of the file’s lines at the time of the last refresh.

Refreshes are called when this class is created, or when manually triggered by refresh().

Returns:the cache
Return type:list
write_to_file(string)

Write to the file.

Note

Please ensure that what you are writing to the file is a string.

Parameters:

string (str) – What to write to the file.

Raises:
  • PermissionError – If you don’t have needed permission to access the file
  • TypeError – If you pass an unsupported type to be written
Returns:

None

Return type:

NoneType

wrap_file()

Shortcut for get_file().wrap().

See also

filehandlers.AbstractFile.wrap()

return:Wrapped file
rtype:io.TextIOWrapper
clear_file()

Clear the file.

Warning

You may not be able to recover the old contents!

Returns:None
Return type:NoneType
Raises:PermissionError – If you don’t have needed permission to access the file
get_file_contents_singlestring()

Get the file’s contents, but as one string.

Note

This function does not use the cache.

Returns:The file’s contents
Return type:str
Raises:PermissionError – If you don’t have needed permission to access the file

OpenModes

class filehandlers.OpenModes

enum.Enum() for the different options you can pass to the keyword argument mode in Python’s builtins.open() function.

It can be used like this:

from filehandlers import OpenModes
open("myfile.txt", mode=OpenModes.READ.value)

This can help so you don’t need to remember all the different mode options.

Warning

For the write option, the file will be cleared and then written to. To avoid this, use append instead!

READ = 'r'

Read only access to the file

WRITE = 'w'

Write only access to the file - *see warning above*

CLEAR = 'w'

Clear the file

APPEND = 'a'

Append to the end of the file (also gives read!)

CREATE = 'x'

Create the file - *raises error if file exists*

CREATE_AND_WRITE = 'w+'

Create the file and ready it to be written to

TEXT = 't'

Default

BINARY = 'b'

Open in binary mode

UPDATING = '+'

This will open a file for reading and writing (updating)