urifile package

This package allows to work with files and directories in a cross-platform way. It has been tested on Android and Windows, but should also work on Linux and macOS.

Example for displaying the content of a folder:

from tatogalib.uri_io.urifile import UriFile

uristring = "content://com.android.externalstorage.documents/document/primary%3ADaten"
folder = UriFile(uristring)
children = folder.listdir()
if children is None:
    print("Uri is not a folder")
for urifile in children:
    print(f"name: {urifile.name}")
    print(f"uristring: {urifile.uristring}")
    print(f"isdir: {urifile.isdir()}")
    print(f"size: {urifile.size}")

Example for reading a file:

from tatogalib.uri_io.urifile import UriFile

uristring = "content://com.android.externalstorage.documents/document/primary%3ADaten/test.txt"
myfile = UriFile(uristring)
f = myfile.open("rt", "utf-8-sig")
bytesobj = f.read()
f.close()
print(str(bytesobj)
tatogalib.uri_io.urifile.ospath_to_uristring(ospath)

Converts an os.path to an URI-string (file://) Returns None if conversion is not possible

Parameters:

ospath (str) – The path string

Returns:

The URI-string

Return type:

str or None

tatogalib.uri_io.urifile.uristring_to_ospath(uristring)

Converts a URI-string to an os.path. This will generally only be possible for file:// URI-strings. Returns None if conversion is not possible

Parameters:

uristring (str) – The URI-string

Returns:

The path string

Return type:

str or None

UriFile class

class tatogalib.uri_io.urifile.UriFile(uristring, fnLog=None)

Creates a UriFile which represents a file or a folder

Parameters:
  • uristring (str) – A URI-string representing this UriFile object

  • fnLog (callable) – The callable which is called from the log method It expects a string parameter

copy_to(urifile)

Copy the binary file represented by this UriFile to the file represented by urifile.

Parameters:

urifile (UriFile) – The target file

Returns:

True on success, False on failure

Return type:

boolean

create_file(child_name, replace=False)

Creates a new file with a length of 0 bytes in the folder represented by this UriFile. If this UriFile is not a folder, a NotADirectoryError will be raised.

Parameters:
  • child_name (str) – The name of the file to be created

  • replace (boolean) – When true, an existing file with the child_name will be replaced. When False, a FileExistsError is raised in this case.

Returns:

The newly created file

Return type:

UriFile

delete()

Deletes the file

Returns:

True on success, False on failure

Return type:

boolean

exists()

Checks if the file or folder exists

Returns:

True when exists, False otherwise

Return type:

boolean

find(child_name)

Returns the child file or folder of the folder represented by this UriFile. If this UriFile is not a folder, a NotADirectoryError will be raised.

Parameters:

child_name (str) – The name of the file or folder to find

Returns:

The child if it exists, None otherwise

Return type:

UriFile or None

static from_path(path)

Creates a new instance of Urifile from a pathlib Path. Return None if the Path does not exist. Raises a TypeError when path is not a Path object

Parameters:

path (Path) – The path representing a file or folder

Returns:

A new Urifile or None

Return type:

UriFile or None

get_authorized_uristring()

Get the uristring to access this UriFile based on existing persisted folder permissions. This is only relevant for Android and will return this UriFile’s uristring on other platforms. When no matching permissions are found, None will be returned.

Returns:

uristring for this UriFile or None

get_path()

Gets the pathlib Path of this UriFile. Returns None if the Path cannot be determined.

Returns:

The Path or None

Return type:

Path or None

static get_persisted_permissions()

Get the persisted permissions to files or folders. This is only relevant for Android and will return an empty list on other platforms.

Returns:

list with permissions

get_uristring()

Get the uristring of this UriFile.

Returns:

uristring for this UriFile

Return type:

str

isdir()

Checks if the UriFile represents an existing folder

Returns:

True or False

Return type:

boolean

isfile()

Checks if the UriFile represents an existing file

Returns:

True or False

Return type:

boolean

property lastmodified

The last modification time (int) of the file or folder. It is the amount of seconds since 1970-01-01T00:00:00

listdir()

Returns a list of all UriFiles contained in the folder represented by this UriFile. When there are no children, an empty list is returned. If this UriFile is not a folder, None will be returned.

Returns:

The children of this UriFile

Return type:

A list of UriFiles or None

log(message)

Logs a message to the user code if fnLog was passed to the constructor

Parameters:

message (str) – The message to be logged

property mime_type

The MIME type (e.g. “application/pdf”) of the file. It is None if the MIME type cannot be evaluated.

property name

The name (str) of the file or folder

open(mode, encoding='utf-8', newline=None)

Opens a binary or text stream for reading or writing. The mode parameter must contain the operation and the data type of the stream to open. Valid operations are (r)ead, (w)rite and (a)ppend. Valid data types are (b)inary and (t)ext. encoding is only relevant when opening text streams for reading or writing. newline is only relevant when opening a text stream for writing. When reading from a text stream, newline is ignored and all kinds of newline characters are converted to \n.

Parameters:
  • mode (str) – The mode for opening a stream, e.g. “wt”

  • encoding (str) – The encoding to use for converting the text to bytes, e.g. “utf-8-sig”

  • newline (str) – The characters to mark the end-of-line. It can be \n, \r, \r\n or None. When None, the system default value is used when writing to a text stream.

Returns:

The stream to open

Return type:

UriInputStream, UriTextInputStream, UriOutputStream or UriTextOutputStream

release_persistent_access(read=True, write=True)

Release a permanent access to the file or folder. This is only relevant for Android and is ignored on other platforms.

Parameters:
  • read (bool) – Release read permissions

  • write (bool) – Release write permissions

request_persistent_access(read=True, write=True)

Get permanent access to the file or folder. This is only relevant for Android and is ignored on other platforms.

Parameters:
  • read (bool) – Request read permissions

  • write (bool) – Request write permissions

set_lastmodified(unixtime)

Sets the last modification time (int) of the file or folder. This is currently not working on Android.

Parameters:

unixtime (int) – amount of seconds since 1970-01-01T00:00:00

Returns:

True on success, False on failure

Return type:

boolean

property size

The size (int) of the file in bytes.