Test if path exists. Test if `path` exists as (and in this order): - a local file. - a remote URL that has been downloaded and stored locally in the `DataSource` directory. - a remote URL that has not been downloaded, but is valid and ac
(self, path)
| 425 | return path |
| 426 | |
| 427 | def exists(self, path): |
| 428 | """ |
| 429 | Test if path exists. |
| 430 | |
| 431 | Test if `path` exists as (and in this order): |
| 432 | |
| 433 | - a local file. |
| 434 | - a remote URL that has been downloaded and stored locally in the |
| 435 | `DataSource` directory. |
| 436 | - a remote URL that has not been downloaded, but is valid and |
| 437 | accessible. |
| 438 | |
| 439 | Parameters |
| 440 | ---------- |
| 441 | path : str or pathlib.Path |
| 442 | Can be a local file or a remote URL. |
| 443 | |
| 444 | Returns |
| 445 | ------- |
| 446 | out : bool |
| 447 | True if `path` exists. |
| 448 | |
| 449 | Notes |
| 450 | ----- |
| 451 | When `path` is a URL, `exists` will return True if it's either |
| 452 | stored locally in the `DataSource` directory, or is a valid remote |
| 453 | URL. `DataSource` does not discriminate between the two, the file |
| 454 | is accessible if it exists in either location. |
| 455 | |
| 456 | """ |
| 457 | |
| 458 | # First test for local path |
| 459 | if os.path.exists(path): |
| 460 | return True |
| 461 | |
| 462 | # We import this here because importing urllib is slow and |
| 463 | # a significant fraction of numpy's total import time. |
| 464 | from urllib.error import URLError |
| 465 | from urllib.request import urlopen |
| 466 | |
| 467 | # Test cached url |
| 468 | upath = self.abspath(path) |
| 469 | if os.path.exists(upath): |
| 470 | return True |
| 471 | |
| 472 | # Test remote url |
| 473 | if self._isurl(path): |
| 474 | try: |
| 475 | netfile = urlopen(path) |
| 476 | netfile.close() |
| 477 | del netfile |
| 478 | return True |
| 479 | except URLError: |
| 480 | return False |
| 481 | return False |
| 482 | |
| 483 | def open(self, path, mode='r', encoding=None, newline=None): |
| 484 | """ |