file_fs¶
File-based FileSystem implementation using the local filesystem.
This module provides FileFs, the default FileSystem backend which operates on plain local paths. Since files are already local, as_local() returns the path unchanged and blocks can be read directly from the file.
Example
import os
import tempfile
from py_misc_utils.fs import file_fs
root = tempfile.mkdtemp()
path = os.path.join(root, 'f.txt')
with open(path, 'w') as fd:
fd.write('hello')
fs = file_fs.FileFs()
print(fs.stat(path).st_size)
FileReader
¶
Reader producing blocks of data from a local file.
Source code in py_misc_utils/fs/file_fs.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
tag(sres)
classmethod
¶
Build a cache tag from a stat result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sres
|
os.stat_result of the file. |
required |
Returns:
| Type | Description |
|---|---|
|
The cache tag embedding size and modification time. |
Source code in py_misc_utils/fs/file_fs.py
46 47 48 49 50 51 52 53 54 55 56 | |
support_blocks()
¶
Return True since local files support direct block reads.
Source code in py_misc_utils/fs/file_fs.py
58 59 60 | |
read_block(bpath, offset, size)
¶
Read a block of data from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bpath
|
Path of the file to read from. |
required | |
offset
|
Offset in the file where reading starts. |
required | |
size
|
Number of bytes to read. |
required |
Returns:
| Type | Description |
|---|---|
|
The requested data as bytes. |
Source code in py_misc_utils/fs/file_fs.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
FileFs
¶
Bases: FsBase
FileSystem implementation for local paths.
It is the default backend and the reference implementation of the FsBase interface: every operation maps directly onto the os module calls of the same name.
Source code in py_misc_utils/fs/file_fs.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
norm_url(url)
¶
Normalize a local path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
The path to normalize. |
required |
Returns:
| Type | Description |
|---|---|
|
The normalized path, as per fs_utils.normpath(). |
Source code in py_misc_utils/fs/file_fs.py
113 114 115 116 117 118 119 120 121 122 | |
stat(url)
¶
Stat a path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
The path to stat. |
required |
Returns:
| Type | Description |
|---|---|
|
The DirEntry of the file, built from the os.stat result. |
Source code in py_misc_utils/fs/file_fs.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
open(url, mode, **kwargs)
¶
Open a local file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
The path of the file to open. |
required | |
mode
|
Open mode string. |
required | |
**kwargs
|
Extra open() keyword arguments; accepted for interface compatibility but not forwarded to the builtin open(). |
{}
|
Returns:
| Type | Description |
|---|---|
|
The opened file object. |
Source code in py_misc_utils/fs/file_fs.py
147 148 149 150 151 152 153 154 155 156 157 158 159 | |
remove(url)
¶
Remove a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
The path of the file to remove. |
required |
Source code in py_misc_utils/fs/file_fs.py
161 162 163 164 165 166 167 | |
rename(src_url, dest_url)
¶
Rename a file or directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_url
|
The source path. |
required | |
dest_url
|
The destination path. |
required |
Source code in py_misc_utils/fs/file_fs.py
169 170 171 172 173 174 175 176 | |
replace(src_url, dest_url)
¶
Rename a file or directory, replacing the destination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_url
|
The source path. |
required | |
dest_url
|
The destination path. |
required |
Source code in py_misc_utils/fs/file_fs.py
178 179 180 181 182 183 184 185 | |
mkdir(url, mode=None)
¶
Create a directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
The path of the directory to create. |
required | |
mode
|
Optional permission mode (default: 0o777). |
None
|
Source code in py_misc_utils/fs/file_fs.py
187 188 189 190 191 192 193 194 | |
makedirs(url, mode=None, exist_ok=None)
¶
Create a directory and all its parents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
The path of the directory to create. |
required | |
mode
|
Optional permission mode (default: 0o777). |
None
|
|
exist_ok
|
If True, do not raise when the directory already exists (default: False). |
None
|
Source code in py_misc_utils/fs/file_fs.py
196 197 198 199 200 201 202 203 204 205 | |
rmdir(url)
¶
Remove an empty directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
The path of the directory to remove. |
required |
Source code in py_misc_utils/fs/file_fs.py
207 208 209 210 211 212 213 | |
rmtree(url, ignore_errors=None)
¶
Recursively remove a directory tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
The path of the directory tree to remove. |
required | |
ignore_errors
|
If True, errors raised while removing the tree are ignored (default: False). |
None
|
Source code in py_misc_utils/fs/file_fs.py
215 216 217 218 219 220 221 222 223 | |
list(url)
¶
Enumerate the entries of a directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
The path of the directory to enumerate. |
required |
Yields:
| Type | Description |
|---|---|
|
A DirEntry for every file and subdirectory of the directory. |
Source code in py_misc_utils/fs/file_fs.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
put_file(url, data_gen)
¶
Write data chunks into a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
The path of the file to write. |
required | |
data_gen
|
Iterable of bytes chunks to write. |
required |
Source code in py_misc_utils/fs/file_fs.py
246 247 248 249 250 251 252 253 254 | |
get_file(url)
¶
Read the contents of a file as chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
The path of the file to read. |
required |
Yields:
| Type | Description |
|---|---|
|
The file contents as bytes chunks. |
Source code in py_misc_utils/fs/file_fs.py
256 257 258 259 260 261 262 263 264 265 266 | |
as_local(url, **kwargs)
¶
Return the path unchanged, since files are already local.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
The path of the file. |
required | |
**kwargs
|
Extra keyword arguments, ignored. |
{}
|
Returns:
| Type | Description |
|---|---|
|
The url itself. |
Source code in py_misc_utils/fs/file_fs.py
268 269 270 271 272 273 274 275 276 277 278 | |
link(src_url, dest_url)
¶
Create a hard link.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_url
|
The path of the existing file. |
required | |
dest_url
|
The path of the link to create. |
required |
Source code in py_misc_utils/fs/file_fs.py
280 281 282 283 284 285 286 287 | |
symlink(src_url, dest_url)
¶
Create a symbolic link.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_url
|
The path of the existing file. |
required | |
dest_url
|
The path of the link to create. |
required |
Source code in py_misc_utils/fs/file_fs.py
289 290 291 292 293 294 295 296 | |