watson.filesystem.api

class watson.filesystem.api.Filesystem(backend)[source]

The abstracted api that the user will interface with when dealing with different types of filesystems.

Example:

from watson import filesystem

fs = filesystem.Filesystem(filesystem.backends.Local())
content = fs.read('/path/to/file')
print(content)
append(file, content, options=None)[source]

Appends specific content to a file.

Similar function to write(), except that any content will be appended to the end of the file.

Parameters:
  • file (string) – The file to write to
  • content (mixed) – The content to write to the file.
  • options (dict) – A dict of args that will be passed to open()
copy(path, new_path)[source]

Copies a file/directory to a new location.

Parameters:
  • path (string) – The path to copy to
  • new_path (string) – The new location for the file/directory
create(path, is_dir=False)[source]

Creates a new file/directory.

If any parent directories in the path do not exist they will be created.

Parameters:
  • path (string) – The path to create
  • is_dir (boolean) – Whether or not to create the path as a directory
delete(path)[source]

Deletes a path from the filesystem.

If a directory is specified, its contents will also be removed.

Parameters:path (string) – The path to delete
exists(path)[source]

Verifies if a path exists.

Parameters:path (string) – The path to verify
Returns:Boolean based on whether or not it exists.
move(path, new_path)[source]

Moves a file/directory to a new location.

Parameters:
  • path (string) – The path to move to
  • new_path (string) – The new location for the file/directory
read(file, options=None)[source]

Reads a file (and then closes it).

Parameters:
  • file (string) – The file to read
  • options (dict) – A dict of args that will be passed to open()
write(file, content, options=None)[source]

Writes some specific content to a file.

Performing this call will overwrite any content that exists within the file. If the file does not exist (or the path), it will be created.

Parameters:
  • file (string) – The file to write to
  • content (mixed) – The content to write to the file.
  • options (dict) – A dict of args that will be passed to open()