discordapi.file

Module Contents

Classes

File

Represents file and name.

class discordapi.file.File(obj=None)

Represents file and name.

This object could be constructed by either using a traditional constructor for simple usages, or using .from_path, .from_io, .from_bytes classmethods for explicity. Refer to each methods for detailed usage.

name

string representing the filename. This could be different from the actual file this object is pointing to.

path

An absolute path to the file. This could be None if the instance was constructed from BytesIO or bytes, in which case fileobj should not be None.

fileobj

io object to the actual file content. This could be None if .read() method wasn’t called yet- in which case path should not be None.

Creates File object.

Parameters

obj

This could be str, BytesIO, bytes, bytearray or tuple/list. str will be assumed as a path to the file- while other types except tuple/list would be used as a file content itself.

if the type is tuple/list, it should be formatted as following: (filename, obj) where filename is str and obj is as explained.

if the type is BytesIO/bytes/bytearray and filename is not given, filename will be defaulted as “file”.

Raises
  • ValueError – Raised if the argument doesn’t satisfy the requirements.

  • FileNotFoundError – Raised if obj was str and given path does not exist.

  • IsADirectoryError – Raised if obj was str and given path was a dir.

classmethod from_path(cls, path, name=None)

Creates File object from path.

Parameters
  • path – str object representing the filepath. It can be either relative or absolute.

  • name – str object representing the name.

Raises
  • ValueError – Raised if the argument doesn’t satisfy the requirements.

  • FileNotFoundError – Raised if obj was str and given path does not exist.

  • IsADirectoryError – Raised if obj was str and given path was a dir.

classmethod from_io(cls, name, io)

Creates File object from io.

Parameters
  • name – str object representing the name.

  • io – BytesIO object representing the file to read.

Raises
  • ValueError – Raised if the argument doesn’t satisfy the requirements.

  • FileNotFoundError – Raised if obj was str and given path does not exist.

  • IsADirectoryError – Raised if obj was str and given path was a dir.

classmethod from_bytes(cls, name, byteobj)

Creates File object from bytes.

Parameters
  • name – str object representing the name.

  • byteobj – bytes/bytearray object representing the file to read.

Raises
  • ValueError – Raised if the argument doesn’t satisfy the requirements.

  • FileNotFoundError – Raised if obj was str and given path does not exist.

  • IsADirectoryError – Raised if obj was str and given path was a dir.

get_name(self)
read(self, *args, **kwargs)
_prep_read(self)
_from_path(self, path, name=None)
_from_io(self, name, obj)
_from_bytes(self, name, bytes)