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