def getsize(self, path=None): path = normalize_storage_path(path) children = self.listdir(path) if children: size = 0 for child in children: if path: name = path + '/' + child else: name = child try: info = self.zf.getinfo(name) except KeyError: pass else: size += info.compress_size return size elif path: try: info = self.zf.getinfo(path) return info.compress_size except KeyError: err_path_not_found(path) else: return 0
def getsize(self, path=None): path = normalize_storage_path(path) # obtain value to return size of if path: try: parent, key = self._get_parent(path) value = parent[key] except KeyError: err_path_not_found(path) else: value = self.root # obtain size of value if isinstance(value, self.cls): # total size for directory size = 0 for v in value.values(): if not isinstance(v, self.cls): try: size += buffer_size(v) except TypeError: return -1 return size else: try: return buffer_size(value) except TypeError: return -1
def getsize(self, path=None): store_path = normalize_storage_path(path) fs_path = self.path if store_path: fs_path = os.path.join(fs_path, store_path) if os.path.isfile(fs_path): return os.path.getsize(fs_path) elif os.path.isdir(fs_path): children = os.listdir(fs_path) size = 0 for child in children: child_fs_path = os.path.join(fs_path, child) if os.path.isfile(child_fs_path): size += os.path.getsize(child_fs_path) return size else: err_path_not_found(path)
def open(store, mode='a', **kwargs): """Convenience function to open a group or array using file-mode-like semantics. Parameters ---------- store : MutableMapping or string Store or path to directory in file system or name of zip file. mode : {'r', 'r+', 'a', 'w', 'w-'}, optional Persistence mode: 'r' means read only (must exist); 'r+' means read/write (must exist); 'a' means read/write (create if doesn't exist); 'w' means create (overwrite if exists); 'w-' means create (fail if exists). **kwargs Additional parameters are passed through to :func:`zarr.open_array` or :func:`zarr.open_group`. See Also -------- zarr.open_array, zarr.open_group Examples -------- Storing data in a directory 'data/example.zarr' on the local file system:: >>> import zarr >>> store = 'data/example.zarr' >>> zw = zarr.open(store, mode='w', shape=100, dtype='i4') # open new array >>> zw <zarr.core.Array (100,) int32> >>> za = zarr.open(store, mode='a') # open existing array for reading and writing >>> za <zarr.core.Array (100,) int32> >>> zr = zarr.open(store, mode='r') # open existing array read-only >>> zr <zarr.core.Array (100,) int32 read-only> >>> gw = zarr.open(store, mode='w') # open new group, overwriting previous data >>> gw <zarr.hierarchy.Group '/'> >>> ga = zarr.open(store, mode='a') # open existing group for reading and writing >>> ga <zarr.hierarchy.Group '/'> >>> gr = zarr.open(store, mode='r') # open existing group read-only >>> gr <zarr.hierarchy.Group '/' read-only> """ path = kwargs.get('path', None) # handle polymorphic store arg store = normalize_store_arg(store, clobber=(mode == 'w')) path = normalize_storage_path(path) if mode in {'w', 'w-', 'x'}: if 'shape' in kwargs: return open_array(store, mode=mode, **kwargs) else: return open_group(store, mode=mode, **kwargs) elif mode == 'a': if contains_array(store, path): return open_array(store, mode=mode, **kwargs) elif contains_group(store, path): return open_group(store, mode=mode, **kwargs) elif 'shape' in kwargs: return open_array(store, mode=mode, **kwargs) else: return open_group(store, mode=mode, **kwargs) else: if contains_array(store, path): return open_array(store, mode=mode, **kwargs) elif contains_group(store, path): return open_group(store, mode=mode, **kwargs) else: err_path_not_found(path)