8
8
from pathlib import Path
9
9
10
10
# for backwards compat, we export cache things from here too
11
- from .caching import ( # noqa: F401
11
+ from fsspec .caching import ( # noqa: F401
12
12
BaseCache ,
13
13
BlockCache ,
14
14
BytesCache ,
15
15
MMapCache ,
16
16
ReadAheadCache ,
17
17
caches ,
18
18
)
19
- from .compression import compr
20
- from .registry import filesystem , get_filesystem_class
21
- from .utils import (
19
+ from fsspec .compression import compr
20
+ from fsspec .config import conf
21
+ from fsspec .registry import filesystem , get_filesystem_class
22
+ from fsspec .utils import (
22
23
_unstrip_protocol ,
23
24
build_name_function ,
24
25
infer_compression ,
@@ -100,7 +101,18 @@ def __repr__(self):
100
101
def __enter__ (self ):
101
102
mode = self .mode .replace ("t" , "" ).replace ("b" , "" ) + "b"
102
103
103
- f = self .fs .open (self .path , mode = mode )
104
+ try :
105
+ f = self .fs .open (self .path , mode = mode )
106
+ except FileNotFoundError as e :
107
+ if has_magic (self .path ):
108
+ raise FileNotFoundError (
109
+ "%s not found. The URL contains glob characters: you maybe needed\n "
110
+ "to pass expand=True in fsspec.open() or the storage_options of \n "
111
+ "your library. You can also set the config value 'open_expand'\n "
112
+ "before import, or fsspec.core.DEFAULT_EXPAND at runtime, to True." ,
113
+ self .path ,
114
+ ) from e
115
+ raise
104
116
105
117
self .fobjects = [f ]
106
118
@@ -396,6 +408,9 @@ def url_to_fs(url, **kwargs):
396
408
return fs , urlpath
397
409
398
410
411
+ DEFAULT_EXPAND = conf .get ("open_expand" , False )
412
+
413
+
399
414
def open (
400
415
urlpath ,
401
416
mode = "rb" ,
@@ -404,6 +419,7 @@ def open(
404
419
errors = None ,
405
420
protocol = None ,
406
421
newline = None ,
422
+ expand = None ,
407
423
** kwargs ,
408
424
):
409
425
"""Given a path or paths, return one ``OpenFile`` object.
@@ -428,6 +444,13 @@ def open(
428
444
newline: bytes or None
429
445
Used for line terminator in text mode. If None, uses system default;
430
446
if blank, uses no translation.
447
+ expand: bool or Nonw
448
+ Whether to regard file paths containing special glob characters as needing
449
+ expansion (finding the first match) or absolute. Setting False allows using
450
+ paths which do embed such characters. If None (default), this argument
451
+ takes its value from the DEFAULT_EXPAND module variable, which takes
452
+ its initial value from the "open_expand" config value at startup, which will
453
+ be False if not set.
431
454
**kwargs: dict
432
455
Extra options that make sense to a particular storage connection, e.g.
433
456
host, port, username, password, etc.
@@ -456,8 +479,7 @@ def open(
456
479
- For implementations in separate packages see
457
480
https://filesystem-spec.readthedocs.io/en/latest/api.html#other-known-implementations
458
481
"""
459
- kw = {"expand" : False }
460
- kw .update (kwargs )
482
+ expand = DEFAULT_EXPAND if expand is None else expand
461
483
out = open_files (
462
484
urlpath = [urlpath ],
463
485
mode = mode ,
@@ -466,7 +488,8 @@ def open(
466
488
errors = errors ,
467
489
protocol = protocol ,
468
490
newline = newline ,
469
- ** kw ,
491
+ expand = expand ,
492
+ ** kwargs ,
470
493
)
471
494
if not out :
472
495
raise FileNotFoundError (urlpath )
0 commit comments