88from pathlib import Path
99
1010# for backwards compat, we export cache things from here too
11- from .caching import ( # noqa: F401
11+ from fsspec .caching import ( # noqa: F401
1212 BaseCache ,
1313 BlockCache ,
1414 BytesCache ,
1515 MMapCache ,
1616 ReadAheadCache ,
1717 caches ,
1818)
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 (
2223 _unstrip_protocol ,
2324 build_name_function ,
2425 infer_compression ,
@@ -100,7 +101,18 @@ def __repr__(self):
100101 def __enter__ (self ):
101102 mode = self .mode .replace ("t" , "" ).replace ("b" , "" ) + "b"
102103
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
104116
105117 self .fobjects = [f ]
106118
@@ -396,6 +408,9 @@ def url_to_fs(url, **kwargs):
396408 return fs , urlpath
397409
398410
411+ DEFAULT_EXPAND = conf .get ("open_expand" , False )
412+
413+
399414def open (
400415 urlpath ,
401416 mode = "rb" ,
@@ -404,6 +419,7 @@ def open(
404419 errors = None ,
405420 protocol = None ,
406421 newline = None ,
422+ expand = None ,
407423 ** kwargs ,
408424):
409425 """Given a path or paths, return one ``OpenFile`` object.
@@ -428,6 +444,13 @@ def open(
428444 newline: bytes or None
429445 Used for line terminator in text mode. If None, uses system default;
430446 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.
431454 **kwargs: dict
432455 Extra options that make sense to a particular storage connection, e.g.
433456 host, port, username, password, etc.
@@ -456,8 +479,7 @@ def open(
456479 - For implementations in separate packages see
457480 https://filesystem-spec.readthedocs.io/en/latest/api.html#other-known-implementations
458481 """
459- kw = {"expand" : False }
460- kw .update (kwargs )
482+ expand = DEFAULT_EXPAND if expand is None else expand
461483 out = open_files (
462484 urlpath = [urlpath ],
463485 mode = mode ,
@@ -466,7 +488,8 @@ def open(
466488 errors = errors ,
467489 protocol = protocol ,
468490 newline = newline ,
469- ** kw ,
491+ expand = expand ,
492+ ** kwargs ,
470493 )
471494 if not out :
472495 raise FileNotFoundError (urlpath )
0 commit comments