@@ -68,6 +68,7 @@ def __init__(
6868 encrypt = None ,
6969 share_access = None ,
7070 register_session_retries = 5 ,
71+ auto_mkdir = False ,
7172 ** kwargs ,
7273 ):
7374 """
@@ -102,6 +103,10 @@ def __init__(
102103 - 'r': Allow other handles to be opened with read access.
103104 - 'w': Allow other handles to be opened with write access.
104105 - 'd': Allow other handles to be opened with delete access.
106+ auto_mkdir: bool
107+ Whether, when opening a file, the directory containing it should
108+ be created (if it doesn't already exist). This is assumed by pyarrow
109+ and zarr-python code.
105110 """
106111 super ().__init__ (** kwargs )
107112 self .host = host
@@ -113,6 +118,7 @@ def __init__(
113118 self .temppath = kwargs .pop ("temppath" , "" )
114119 self .share_access = share_access
115120 self .register_session_retries = register_session_retries
121+ self .auto_mkdir = auto_mkdir
116122 self ._connect ()
117123
118124 @property
@@ -224,6 +230,8 @@ def _open(
224230 By specifying 'share_access' in 'kwargs' it is possible to override the
225231 default shared access setting applied in the constructor of this object.
226232 """
233+ if self .auto_mkdir and "w" in mode :
234+ self .makedirs (self ._parent (path ), exist_ok = True )
227235 bls = block_size if block_size is not None and block_size >= 0 else - 1
228236 wpath = _as_unc_path (self .host , path )
229237 share_access = kwargs .pop ("share_access" , self .share_access )
@@ -245,6 +253,8 @@ def copy(self, path1, path2, **kwargs):
245253 """Copy within two locations in the same filesystem"""
246254 wpath1 = _as_unc_path (self .host , path1 )
247255 wpath2 = _as_unc_path (self .host , path2 )
256+ if self .auto_mkdir :
257+ self .makedirs (self ._parent (path2 ), exist_ok = True )
248258 smbclient .copyfile (wpath1 , wpath2 , port = self ._port , ** kwargs )
249259
250260 def _rm (self , path ):
0 commit comments