Open and return file-like object. If `path` is a URL, it will be downloaded, stored in the `DataSource` directory and opened from there. Parameters ---------- path : str or pathlib.Path Local file path or URL to open. mode : {'r'
(self, path, mode='r', encoding=None, newline=None)
| 481 | return False |
| 482 | |
| 483 | def open(self, path, mode='r', encoding=None, newline=None): |
| 484 | """ |
| 485 | Open and return file-like object. |
| 486 | |
| 487 | If `path` is a URL, it will be downloaded, stored in the |
| 488 | `DataSource` directory and opened from there. |
| 489 | |
| 490 | Parameters |
| 491 | ---------- |
| 492 | path : str or pathlib.Path |
| 493 | Local file path or URL to open. |
| 494 | mode : {'r', 'w', 'a'}, optional |
| 495 | Mode to open `path`. Mode 'r' for reading, 'w' for writing, |
| 496 | 'a' to append. Available modes depend on the type of object |
| 497 | specified by `path`. Default is 'r'. |
| 498 | encoding : {None, str}, optional |
| 499 | Open text file with given encoding. The default encoding will be |
| 500 | what `open` uses. |
| 501 | newline : {None, str}, optional |
| 502 | Newline to use when reading text file. |
| 503 | |
| 504 | Returns |
| 505 | ------- |
| 506 | out : file object |
| 507 | File object. |
| 508 | |
| 509 | """ |
| 510 | |
| 511 | # TODO: There is no support for opening a file for writing which |
| 512 | # doesn't exist yet (creating a file). Should there be? |
| 513 | |
| 514 | # TODO: Add a ``subdir`` parameter for specifying the subdirectory |
| 515 | # used to store URLs in self._destpath. |
| 516 | |
| 517 | if self._isurl(path) and self._iswritemode(mode): |
| 518 | raise ValueError("URLs are not writeable") |
| 519 | |
| 520 | # NOTE: _findfile will fail on a new file opened for writing. |
| 521 | found = self._findfile(path) |
| 522 | if found: |
| 523 | _fname, ext = self._splitzipext(found) |
| 524 | if ext == 'bz2': |
| 525 | mode.replace("+", "") |
| 526 | return _file_openers[ext](found, mode=mode, |
| 527 | encoding=encoding, newline=newline) |
| 528 | else: |
| 529 | raise FileNotFoundError(f"{path} not found.") |
| 530 | |
| 531 | |
| 532 | class Repository (DataSource): |