Apache OpenDAL™ Python binding#
Installation#
1 | |
Local Usage#
Developer must set two required arguments to work with files locally:
- scheme: which should be specified as fs
- root: where OpenDAl considers the root of the directory for operations will be.
For example in the following operator:
opendal.Operator("fs", root="/foo")
OpenDAL considers /foo to be the root of all paths, and that means that we can access paths inside of /foo without specifying anything else.
If /foo happens to contain the file baz.txt, we can simply call .list("/")
We can see this in the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
When running we get the following output:
1 2 | |
If we want full access to our file system, we can specify a root of "/", but note that for any operations you will always need to specify the full path to a file or directory.
Reading#
There are two ways to read data using OpenDAL. One way is to use the read method on an operator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Yields the following:
1 | |
Note that the output is bytes, but we can get it as a string by simply calling .decode() on the data we read. All reads with OpenDAL return bytes.
Now lets use the open method on an operator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
This again yields
1 | |
Again, note that all reads with OpenDAL return bytes, so specifying a mode of "r" will raise an exception.
Writing#
Now that we know how to read data, let's replace the Pathlib code above with OpenDAL using the write method:
1 2 3 4 5 6 7 8 9 | |
This yields the following:
1 | |
And again, but using the open method:
1 2 3 4 5 6 7 8 9 10 | |
This again yields:
1 | |
Again, note that all writing happens in bytes and a mode of "w" will raise an exception.
Async#
OpenDAL supports async operation on all operator methods. One can simply replace the Operator with an AsyncOperator and await on method calls. The below example illustrates this behavior:
Standard API:
1 2 3 4 5 6 | |
Async API equivalent:
1 2 3 4 5 6 7 8 | |