Class FileIO

class ooodev.utils.file_io.FileIO[source]
classmethod append_to(fnm, msg)[source]

Appends text to a file

Parameters:
  • fnm (PathOrStr) – File to append text to.

  • msg (str) – Text to append.

Raises:

Exception – If unable to append text.

Return type:

None

static create_temp_file(im_format)[source]

Creates a temporary file

Parameters:

im_format (str) – File suffix such as txt or cfg

Raises:
  • ValueError – If im_format is empty.

  • Exception – If creation of temp file fails.

Returns:

Path to temp file.

Return type:

str

Note

Temporary file is created in system temp directory. Caller of this method is responsible for deleting the file.

classmethod delete_file(fnm)[source]

Deletes a file

Parameters:

fnm (PathOrStr) – file to delete

Returns:

True if delete is successful; Otherwise, False

Return type:

bool

classmethod delete_files(*fnms)[source]

Deletes files

Parameters:

fnms (str) – one or more files to delete

Returns:

Returns True if all file are deleted; Otherwise, False

Return type:

bool

static expand_macro(fnm)[source]

Expands macros in a file path if needed

Parameters:

fnm (str) – file path

Returns:

expanded path

Return type:

str

Example

>>> print(FileIO.expand_macro("vnd.sun.star.expand:$BUNDLED_EXTENSIONS/wiki-publisher/templates"))
'file:///usr/lib/libreoffice/share/extensions/wiki-publisher/templates'

New in version 0.11.14.

classmethod fnm_to_url(fnm)[source]

Converts file path to url

Parameters:

fnm (PathOrStr) – file path

Raises:

Exception – If unable to get url form fnm.

Returns:

Converted path.

Return type:

str

static get_absolute_path(fnm)[source]

Gets Absolute path

Parameters:

fnm (PathOrStr) – path as string

Returns:

absolute path

Return type:

Path

static get_ext(fnm)[source]

Gets file extension without the .

Parameters:

fnm (PathOrStr) – file path

Raises:

ValueError – If fnm is empty string

Returns:

Extension if Found; Otherwise, None

Return type:

str | None

classmethod get_file_names(dir)[source]

Gets a list of filenames in a folder

Parameters:

dir (PathOrStr) – Folder path

Returns:

List of files.

Return type:

List[str]

See also

get_file_paths()

classmethod get_file_paths(dir)[source]

Gets a generator of file paths in a folder

Parameters:

dir (PathOrStr) – Folder path

Yields:

Generator[Path, None, None] – Generator of Path objects

Return type:

Generator[Path, None, None]

See also

get_file_paths()

Return type:

Generator[Path, None, None]

Parameters:

dir (PathOrStr) –

static get_fnm(path)[source]

Gets last part of a file or dir such as myfile.txt

Parameters:

path (PathOrStr) – file path

Returns:

file name portion

Return type:

str

classmethod get_mime_type(zfa)[source]

Gets Mime-type for zip file access

Parameters:

zfa (XZipFileAccess) – zip file access

Raises:

Exception – If error getting Mime-type

Returns:

Mime-type if found; Otherwise, None

Return type:

str | None

static get_utils_folder()[source]

Gets path to utils folder

Returns:

folder path as str

Return type:

str

classmethod is_exist_dir(dnm)[source]
classmethod is_exist_dir(dnm, raise_err)
classmethod is_exist_dir(dnm, raise_err=False)

Gets is a directory actually exist.

Ensures that dnm is a valid PathOrStr format.

Ensures that dnm is an existing directory.

Parameters:
  • dnm (PathOrStr) – directory to check. Relative paths are accepted

  • raise_err (bool, optional) – Determines if an error is raised. Defaults to False.

Raises:
  • ValueError – If raise_err is True and dnm is not a valid PathOrStr format.

  • FileNotFoundError – If raise_err is True and dir is not found.

  • NotADirectoryError – If raise_err is True and dnm is not a directory.

Returns:

True if file is valid; Otherwise, False.

Return type:

bool

classmethod is_exist_file(fnm)[source]
classmethod is_exist_file(fnm, raise_err)
classmethod is_exist_file(fnm, raise_err=False)

Gets is a file actually exist.

Ensures that fnm is a valid PathOrStr format.

Ensures that fnm is an existing file.

Parameters:
  • fnm (PathOrStr) – File to check. Relative paths are accepted

  • raise_err (bool, optional) – Determines if an error is raised. Defaults to False.

Raises:
  • ValueError – If raise_err is True and fnm is not a valid PathOrStr format.

  • ValueError – If raise_err is True and fnm is not a file.

  • FileNotFoundError – If raise_err is True and file is not found

Returns:

True if file is valid; Otherwise, False.

Return type:

bool

classmethod is_openable(fnm)[source]

Gets if a file can be opened

Parameters:

fnm (PathOrStr) – file path

Returns:

True if file can be opened; Otherwise, False

Return type:

bool

static is_valid_path_or_str(fnm)[source]

Checks fnm it make sure it is a valid path or string.

Parameters:

fnm (PathOrStr) – Input path

Returns:

False if fnm is None or empty string; Otherwise; True

Return type:

bool

classmethod make_directory(dir: Any)[source]
classmethod make_directory(fnm: Any)
classmethod make_directory(*args, **kwargs)

Creates path and sub paths they do not exist.

Parameters:
  • dir (PathOrStr) – PathLike object to a directory

  • fnm (PathOrStr) – PathLike object to a directory or file.

Returns:

Path of directory.

Return type:

Path

Notes

If a file path is passed in then a directory is created for the file’s parent.

static read_lines(in_stream)[source]

Converts a input stream to a list of strings.

Parameters:

in_stream (XInputStream) – Input stream

Returns:

If text was found in input stream the list of string; Otherwise, None

Return type:

List[str] | None

classmethod save_array(fnm, arr)[source]

Saves a 2d array to a file as tab delimited data.

Parameters:
  • fnm (PathOrStr) – file to save data to

  • arr (Table) – 2d array of data.

Return type:

None

classmethod save_bytes(fnm, b)[source]

Writes bytes data to a given file.

Parameters:
  • fnm (PathOrStr) – File to write data

  • b (bytes) – data to write

Raises:
  • ValueError – If b is None

  • Exception – If any other error occurs.

Return type:

None

classmethod save_string(fnm, data)[source]

Writes string of data into given file.

Parameters:
  • fnm (PathOrStr) – File to write string

  • data (str) – String to write in file

Raises:

Exception – If Error occurs

Return type:

None

static unzip_file(zfa, fnm)[source]

Unzip File. Not yet implemented

Parameters:
  • zfa (XZipFileAccess) – Zip File Access

  • fnm (PathOrStr) – File path

Raises:

NotImplementedError

Return type:

None

static uri_absolute(uri)[source]

Ensures a string uri is absolute.

Parameters:

uri (str) – _description_

Returns:

Absolute uri

Return type:

str

classmethod uri_to_path(uri_fnm)[source]
classmethod uri_to_path(uri_fnm, ensure_absolute)
classmethod uri_to_path(uri_fnm, ensure_absolute=True)

Converts uri file to path.

Parameters:
  • uri_fnm (PathOrStr) – URI to convert

  • ensure_absolute (bool) – If True then ensures that the return path is absolute. Default is True

Raises:

ConvertPathError – If unable to convert.

Returns:

Converted URI as path.

Return type:

Path

classmethod url_to_path(url)[source]

Converts url to path

Parameters:

url (str) – url to convert

Raises:

Exception – If unable to parse url.

Returns:

path as string

Return type:

Path

classmethod zip_access(fnm)[source]
Return type:

XZipFileAccess

Parameters:

fnm (PathOrStr) –

classmethod zip_list(fnm)[source]

Prints info to console for a give zip file.

Parameters:

fnm (PathOrStr) – Path to zip file.

Return type:

None

classmethod zip_list_uno(fnm)[source]

Use zip_list method

Return type:

None

Parameters:

fnm (PathOrStr) –