@@ -966,12 +966,12 @@ def listdir(
966966 path = re .sub (r'^(\./|/)+' , r'' , str (path ))
967967 path = re .sub (r'/+$' , r'' , path ) + '/'
968968
969- # Single validation GET (info) rather than is_dir + info later
970- info = self . info ( path )
971- if info . type != 'directory' :
972- raise NotADirectoryError ( f'path is not a directory: { path } ' )
973-
974- out = self . _listdir ( path , recursive = recursive , return_meta = return_meta )
969+ # Validate via listing GET; if response lacks 'content', it's not a directory
970+ try :
971+ out = self . _listdir ( path , recursive = recursive , return_meta = return_meta )
972+ except Exception as exc :
973+ # If the path doesn't exist or isn't a directory, _listdir will fail
974+ raise NotADirectoryError ( f' path is not a directory: { path } ' ) from exc
975975 if path != '/' :
976976 path_n = len (path .split ('/' )) - 1
977977 if return_meta :
@@ -990,6 +990,7 @@ def download_file(
990990 * ,
991991 overwrite : bool = False ,
992992 encoding : Optional [str ] = None ,
993+ _skip_dir_check : bool = False ,
993994 ) -> Optional [Union [bytes , str ]]:
994995 """
995996 Download the content of a file path.
@@ -1013,7 +1014,7 @@ def download_file(
10131014 """
10141015 if local_path is not None and not overwrite and os .path .exists (local_path ):
10151016 raise OSError ('target file already exists; use overwrite=True to replace' )
1016- if self .is_dir (path ):
1017+ if not _skip_dir_check and self .is_dir (path ):
10171018 raise IsADirectoryError (f'file path is a directory: { path } ' )
10181019
10191020 out = self ._manager ._get (
@@ -1054,11 +1055,7 @@ def download_folder(
10541055 if local_path is not None and not overwrite and os .path .exists (local_path ):
10551056 raise OSError ('target path already exists; use overwrite=True to replace' )
10561057
1057- # Validate directory with single info call
1058- info = self .info (path )
1059- if info .type != 'directory' :
1060- raise NotADirectoryError (f'path is not a directory: { path } ' )
1061-
1058+ # listdir validates directory; no extra info call needed
10621059 entries = self .listdir (path , recursive = True , return_meta = True )
10631060 for entry in entries :
10641061 # Each entry is a dict with path relative to root and type
@@ -1073,7 +1070,7 @@ def download_folder(
10731070 remote_path = os .path .join (path , rel_path )
10741071 target_file = os .path .normpath (os .path .join (local_path , rel_path ))
10751072 os .makedirs (os .path .dirname (target_file ), exist_ok = True )
1076- self .download_file (remote_path , target_file , overwrite = overwrite )
1073+ self .download_file (remote_path , target_file , overwrite = overwrite , _skip_dir_check = True )
10771074
10781075 def remove (self , path : PathLike ) -> None :
10791076 """
0 commit comments