File

class tekdrive.models.File(tekdrive: TekDrive, id: Optional[str] = None, _data: Optional[Dict[str, Any]] = None)

A class representing a TekDrive file.

Examples

Load a file by id:

file_id = "9606be66-8af0-42fc-b199-7c0ca7e30d73"
file = td.file(file_id)
bytes

File size in bytes.

Type

str

created_at

When the file was created.

Type

datetime

creator

File creator.

Type

Partial User

file_type

File type such as "JPG" or "WFM".

Type

str

id

Unique ID of the file.

Type

str

name

Name of the file.

Type

str

owner

File owner.

Type

Partial User

parent_folder_id

Unique ID of the file’s parent folder.

Type

str

shared_at

When the file was shared with the requesting user. Will be None if the user has direct access.

Type

datetime, optional

type

Type of TekDrive object - will always be "FILE".

Type

str

permissions

File permissions for the requesting user.

Type

Permissions

updated_at

When the file was last updated.

Type

datetime, optional

add_member(username: Optional[str] = None, user_id: Optional[str] = None, edit_access: bool = False) → tekdrive.models.drive.member.Member

Share the file with an existing or new user.

Parameters
  • username – The username (email) of the sharee.

  • user_id – The user ID of the sharee.

  • edit_access – Give member edit access?

Examples

Share with read only permissions (default):

file_member = file.add_member(username="read_only@example.com")

Share with edit permissions:

file_member = file.add_member(user_id="354bcafb-6c54-4a1f-9b94-a76f38b548e5", edit_access=True)
Returns

Member

artifact(artifact_id: str, depth: int = 1) → tekdrive.models.drive.artifact.Artifact

Get a file artifact by ID.

Parameters
  • artifact_id – Unique ID for the artifact

  • depth – How many nested levels of child artifacts to return.

Examples

Load artifact with children up to 3 levels deep:

artifact_id = "017820c4-03ba-4e9d-be2f-e0ba346ddd9b"
artifact = file.artifact(artifact_id, depth=3)
Returns

Artifact

artifacts(flat: bool = False) → tekdrive.models.drive.artifact.ArtifactsList

Get a list of file artifacts.

Parameters

flat – Return artifacts in flat list with no child nesting?

Examples

Iterate over all file artifacts:

for artifact in file.artifacts():
    print(artifact.name)
Returns

List [ Artifact ]

delete(hard_delete: bool = False) → None

Delete the file, by default it will be placed in the user’s trashcan.

Parameters

hard_delete – Permanently delete the file?

Examples

Delete - placing in trash:

file.delete()

Delete - immediately delete:

file.delete(hard_delete=True)
download(path_or_writable: Optional[Union[str, IO]] = None) → None

Download contents.

Parameters

path_or_writable – Path to a local file or a writable stream where contents will be written.

Raises

ClientException – If invalid file path is given.

Examples

Download to local file using path:

here = os.path.dirname(__file__)
contents_path = os.path.join(here, "test_file_overwrite.txt")
file.download(contents_path)

Download using writable stream:

with open("./download.csv", "wb") as f:
    file.download(f)
members() → tekdrive.models.drive.member.MembersList

Get a list of file members.

Examples

Iterate over all file members:

for member in file.members():
    print(member.username)
Returns

List [ Member ]

modify_member(user_id: str, edit_access: bool) → tekdrive.models.drive.member.Member

Modify an existing file member.

Parameters
  • user_id – The user ID of the member.

  • edit_access – Give member edit access?

Examples

Grant edit access:

updated_file_member = file.modify_member(user_id="354bcafb-6c54-4a1f-9b94-a76f38b548e5", edit_access=True)

Revoke edit access:

updated_file_member = file.modify_member(user_id="354bcafb-6c54-4a1f-9b94-a76f38b548e5", edit_access=False)
Returns

Member

move(parent_folder_id: str) → None

Move file to a different folder.

Parameters

parent_folder_id – Unique ID of folder to move the file into.

Examples

Move by ID:

folder_id = "3b525331-9da7-4e8d-b045-4acdba8d9dc7"
file.move(folder_id)

Move to newly created folder:

folder = td.folder.create("FolderA")
file.move(folder.id)
remove_member(user_id: str) → None

Revoke access for a current file member.

Parameters

user_id – The user ID of the member.

Examples

Remove member:

file.remove_member(user_id="354bcafb-6c54-4a1f-9b94-a76f38b548e5")
restore() → None

Restore the file from user’s trashcan.

Examples

Restore - remove from trash:

file.restore()
save() → None

Save any changes to file meta. Supported attributes: name.

Examples

Rename a file:

file.name = "my_new_name"
file.save()
upload(path_or_readable: Union[str, IO]) → None

Upload file contents. This will overwrite existing content, if any.

Parameters

path_or_readable – Path to a local file or a readable stream representing the contents to upload.

Raises

ClientException – If invalid file path is given.

Examples

Upload using path:

here = os.path.dirname(__file__)
contents_path = os.path.join(here, "test_file_overwrite.txt")
file.upload(contents_path)

Upload using readable stream:

with open("./test_file.txt", "rb") as f:
    new_file.upload(f)