You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: bindings/python/docs/index.md
+140-2Lines changed: 140 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,145 @@
6
6
pip install opendal
7
7
```
8
8
9
-
## Usage
9
+
## Local Usage
10
+
Developer must set two required arguments to work with files locally:
11
+
-`scheme`: which should be specified as `fs`
12
+
-`root`: where OpenDAl considers the root of the directory for operations will be.
13
+
14
+
For example in the following operator:
15
+
`opendal.Operator("fs", root="/foo")`
16
+
17
+
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.
18
+
If `/foo` happens to contain the file `baz.txt`, we can simply call `.list("/")`
19
+
20
+
We can see this in the following example:
21
+
22
+
```python
23
+
from opendal import Operator
24
+
from pathlib import Path
25
+
import os
26
+
27
+
28
+
os.makedirs("foo", exist_ok=True)
29
+
30
+
file= Path("foo", "bar.txt")
31
+
file.touch()
32
+
33
+
op = Operator("fs", root=Path("foo").name)
34
+
forfilein op.list("/"):
35
+
print(file)
36
+
```
37
+
38
+
When running we get the following output:
39
+
40
+
```
41
+
/
42
+
bar.txt
43
+
```
44
+
45
+
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.
46
+
47
+
### Reading
48
+
There are two ways to read data using OpenDAL. One way is to use the `read` method on an operator:
49
+
```python
50
+
from opendal import Operator
51
+
from pathlib import Path
52
+
import os
53
+
54
+
55
+
os.makedirs("foo", exist_ok=True)
56
+
57
+
file= Path("foo", "bar.txt")
58
+
file.touch()
59
+
file.write_text("baz")
60
+
61
+
62
+
op = Operator("fs", root=Path("foo").name)
63
+
64
+
data = op.read(file.name)
65
+
print(data)
66
+
```
67
+
68
+
Yields the following:
69
+
```
70
+
b'baz'
71
+
```
72
+
73
+
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.
74
+
75
+
Now lets use the `open` method on an operator:
76
+
```python
77
+
from opendal import Operator
78
+
from pathlib import Path
79
+
import os
80
+
81
+
82
+
os.makedirs("foo", exist_ok=True)
83
+
84
+
file= Path("foo", "bar.txt")
85
+
file.touch()
86
+
file.write_text("baz")
87
+
88
+
89
+
op = Operator("fs", root=Path("foo").name)
90
+
91
+
with op.open(file.name, "rb") as f:
92
+
print(f.read())
93
+
```
94
+
95
+
This again yields
96
+
```
97
+
b'baz'
98
+
```
99
+
100
+
Again, note that all reads with OpenDAL return bytes, so specifying a mode of `"r"` will raise an exception.
101
+
102
+
103
+
### Writing
104
+
Now that we know how to read data, let's replace the `Pathlib` code above with OpenDAL using the `write` method:
105
+
```python
106
+
from opendal import Operator
107
+
from pathlib import Path
108
+
109
+
110
+
op = Operator("fs", root=Path("foo").name)
111
+
112
+
op.write("baz.txt", "my amazing data".encode())
113
+
114
+
print(op.read("baz.txt").decode())
115
+
```
116
+
117
+
This yields the following:
118
+
```
119
+
my amazing data
120
+
```
121
+
122
+
And again, but using the `open` method:
123
+
```python
124
+
from opendal import Operator
125
+
from pathlib import Path
126
+
127
+
128
+
op = Operator("fs", root=Path("foo").name)
129
+
130
+
with op.open("baz.txt", "wb") as f:
131
+
f.write("my amazing data".encode())
132
+
133
+
print(op.read("baz.txt").decode())
134
+
```
135
+
136
+
This again yields:
137
+
138
+
```
139
+
my amazing data
140
+
```
141
+
142
+
Again, note that all writing happens in bytes and a mode of `"w"` will raise an exception.
143
+
144
+
## Async
145
+
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:
0 commit comments