k3fs

pykit3 is is a collection of toolkit in python3.

Documentation for the Code

k3fs is collection of file-system operation utilities.

Usage:

>>> fwrite('/tmp/foo', "content")

>>> fread('/tmp/foo')
'content'

>>> 'foo' in ls_files('/tmp/')
True

Functions

k3fs.ls_dirs(*paths)

Get sorted sub directories of paths.

Parameters

paths – is the directory path.

Returns

of all sub directory names.

Return type

list

k3fs.ls_files(*paths, pattern='.*')

List all files that match pattern in path.

Parameters
  • paths – is a directory path.

  • pattern (str) – is a regular expression that matches wanted file names.

Returns

of sorted file names.

Return type

list

k3fs.makedirs(*paths, **kwargs)

Make directory. If intermediate directory does not exist, create them too.

Parameters
  • paths – is a single part path such as /tmp/foo or a separated path such as (‘/tmp’, ‘foo’).

  • mode (int) – specifies permission mode for the dir created or existed. By defaul it is 0755.

  • uid (int) – specifies uid for the created dir. By default they are None and the created dir inherits ownership from the running python program.

  • gid (int) – specifies uid for the created dir. By default they are None and the created dir inherits ownership from the running python program.

Raises

OSError – if trying to create dir with the same path of a non-dir file, or having other issue like permission denied.

k3fs.fread(*paths, mode='')

Read and return the entire file specified by path

Parameters
  • paths – is the path of the file to read.

  • mode (str) – If mode=’b’ it returns bytes. If mode=’‘ it returns a str decoded from bytes.

Returns

file content in string or bytes.

k3fs.fwrite(*paths_content, uid=None, gid=None, atomic=False, fsync=True)

Write fcont into file path.

Args

paths_content:

is the file path to write to and the content to write. The last elt is content, e.g.: fwrite(‘/tmp’, ‘foo’, ‘bar’) write ‘bar’ into file ‘/tmp/foo’.

uid:

specifies the user_id the file belongs to.

gid:

specifies the group_id the file belongs to.

By default they are None, which means the file that has been written inheirts ownership of the running python script.

atomic(bool):

atomically write fcont to the path.

Write fcont to a temporary file, then rename to the path. The temporary file names of same path in one process distinguish with nanosecond, it is not atomic if the temporary files of same path created at the same nanosecond. The renaming will be an atomic operation (this is a POSIX requirement).

fsync(bool):

specify if need to synchronize data to storage device.

k3fs.remove(*paths, onerror=None)

Recursively delete path, the path is file, directory or symbolic link.

Parameters
  • paths – is the path to remove.

  • onerror (str or callable) –

    • “raise”: when error occur it raises the original error.

    • ”ignore”: ignore error and go on.

    • A callable:

      it is called to handle the error with arguments (func, path, exc_info) where func is os.listdir, os.remove, os.rmdir or os.path.isdir.

Indices and tables