Overview

sieve.File is a utility class that helps with handling files that are inputs to a function, produced locally in the file system, or downloaded from an external url. The underlying contents in a sieve.File are backed by Sieve’s file servers and are downloaded lazily, meaning that they will not download the content until requested with the path property. This means that a sieve.File can be passed around to different functions efficiently without having to deal with network overhead, allowing for more efficnient applications.

class File(sieve.Struct):

A type that represents a file.

Constructor Optional Arguments

  • url (str): The URL of the file to download.
  • path (str): The local path to the file.

A File can be instantiated in two ways, either by providing a local file path, or an external url. You can either pass them in as positional arguments or as keyword arguments to path or url.

import sieve

# Instantiate a sieve File with implied path or url
local_file = sieve.File("path/to/file.txt")
external_file = sieve.File("example.com/file.txt")

# Instantiate a sieve File with a local file path
local_file = sieve.File(path="path/to/file.txt")

# Instantiate a sieve File with a url
external_file = sieve.File(url="example.com/file.txt")

Properties

url

url: str

The url of the file, if it exists. This will return None if the file refers to something on the local filesystem.


import sieve

# Instantiate a File with a url
my_external_file = sieve.File(url="example.com/file.txt")
my_local_file = sieve.File("path/to/file.txt")

# Download the file, open it, and read the contents
print(my_external_file.url) # prints "example.com/file.txt"
print(my_local_file.url) # prints None


### path
```python
@property
def path(self) -> str:

The local path to the file. This will download the file if it is not downloaded already, and return the path of the downloaded file.

import sieve

# Instantiate a File with a url
my_file = sieve.File(url="example.com/file.txt")

# Download the file, open it, and read the contents
opened_file = open(my_file.path, "r")
print(opened_file.read())