Whether the file exists, same usage as `os.path.exists`. Support local path and oss path. Example: from easycv.file import io io.access_oss(your oss config) # only oss file need, refer to `IO.access_oss` ret = io.exists('oss://bucket_name
(self, path)
| 261 | raise ValueError('invalid mode: %s' % mode) |
| 262 | |
| 263 | def exists(self, path): |
| 264 | """ |
| 265 | Whether the file exists, same usage as `os.path.exists`. |
| 266 | Support local path and oss path. |
| 267 | |
| 268 | Example: |
| 269 | from easycv.file import io |
| 270 | io.access_oss(your oss config) # only oss file need, refer to `IO.access_oss` |
| 271 | ret = io.exists('oss://bucket_name/dir') |
| 272 | print(ret) |
| 273 | Args: |
| 274 | path: oss path or local path |
| 275 | """ |
| 276 | if not is_oss_path(path): |
| 277 | return super().exists(path) |
| 278 | |
| 279 | bucket, _path = self._get_bucket_obj_and_path(path) |
| 280 | # TODO: 网络不稳时try,其他错误直接抛出 |
| 281 | if not path.endswith('/'): |
| 282 | # if file exists |
| 283 | exists = self._obj_exists(bucket, _path) |
| 284 | # if bucket exists |
| 285 | if not exists: |
| 286 | _path = self._correct_oss_dir(_path) |
| 287 | exists = self._obj_exists(bucket, _path) |
| 288 | else: |
| 289 | # if bucket exists |
| 290 | exists = self._obj_exists(bucket, _path) |
| 291 | |
| 292 | return exists |
| 293 | |
| 294 | def _obj_exists(self, bucket, path): |
| 295 | return bucket.object_exists(path) |