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