CorrectOCR.fileio module

class CorrectOCR.fileio.FileIO[source]

Bases: object

Various file IO helper methods.

classmethod cachePath(name='')[source]
classmethod get_encoding(file)[source]

Get encoding of a text file.

Parameters

file (Path) – A path to a text file.

Return type

str

Returns

The encoding of the file, eg. ‘utf-8’, ‘Windows-1252’, etc.

classmethod ensure_new_file(path)[source]

Moves a possible existing file out of the way by adding a numeric counter before the extension.

Parameters

path (Path) – The path to check.

classmethod ensure_directories(path)[source]

Ensures that the entire path exists.

Parameters

path (Path) – The path to check.

classmethod copy(src, dest)[source]

Copies a file.

Parameters
  • src (Path) – Source-path.

  • dest (Path) – Destination-path.

classmethod delete(path)[source]

Deletes a file.

Parameters

path (Path) – The path to delete.

classmethod save(data, path, backup=True)[source]

Saves data into a file. The extension determines the method of saving:

  • .pickle – uses pickle.

  • .json – uses json.

  • .csv – uses csv.DictWriter (assumes data is list of vars()-capable objects). The keys of the first object determines the header.

Any other extension will simply write() the data to the file.

Parameters
  • data (Any) – The data to save.

  • path (Path) – The path to save to.

  • backup – Whether to move existing files out of the way via ensure_new_file()

classmethod load(path, default=None)[source]

Loads data from a file. The extension determines the method of saving:

Any other extension will simply read() the data from the file.

Parameters
  • path (Path) – The path to load from.

  • default – If file doesn’t exist, return default instead.

Returns

The data from the file, or the default.