Skip to content

Latest commit

 

History

History
118 lines (64 loc) · 4.44 KB

pythonapi.io.rst

File metadata and controls

118 lines (64 loc) · 4.44 KB

Reading and Writing Files

View the source for the code samples on this page in snippets-pythonapi-io.

Writing Archive Files

To write to a file in DataFS you'll first need to create the archive. We'll assume you've already set up your api with manager, and authorities. We'll use :py~datafs.DataAPI.create.

DataFS needs a file path in order to update files. So now that we have an archive on our manager, we can create a file and put it up in our filesystem. So let's first create a file called sample.txt.

Now that we have a file can we can use the update() method to upload our file. Depending on size of file and network speeds, there may be some latency when calling :py~datafs.core.data_archive_DataArchive.update.

Now that our archive is up let's say we want to now pull it down and read it. Reading an archive file is an interface the python users will recognize. We initialize a context manager using the with statement. The actual call happens through :py~datafs.core.data_archive.DataArchive.open

This is really great. Let's see what happens when we want to make some updates to the file.

Now let's open and read to see.

Looks good!

Since DataFS simply needs a filepath, you can simply provide a filepath and it will upload and write the file. If you have a file locally that you want managed by DataFS you can create an archive and put it on your filesystem.

Downloading

If you want to download the latest version of an archive all you need to do is provide a path and set version='latest'. This will download the latest version to the filepath specified. We'll use :py~datafs.core.data_archive.DataArchive.get_archive to get the archive and then use :py~datafs.core.data_archive.DataArchive.download

Let's just double check that we indeed have our file

Writing Streaming Objects

If you are working with certain packages like pandas, or xarray that need a filepath, the interaction is slightly modified from typical file objects. Let's first create the dataset we want to write to. The method we'll use for this operation is :pydatafs.core.DataArchive.get_local_path and xarray's open_dataset method

Downloading Streaming Objects

Reading a streaming object is similar to reading a regular file object but we generate a file path that is then passed to the package you are using for reading and writing. In this case we are using xarray so we'll use our :py~datafs.core.data_archive.DataArchive.get_local_path and xarray's open_dataset method

Check out examples for more information on how to write and read files DataFS on different filesystems