@@ -1309,26 +1309,24 @@ def copy_into(self, target_dir, exist_ok=True, **kwargs):
13091309 if not name :
13101310 raise ValueError (f"{ self !r} has an empty name" )
13111311
1312- target = ( target_dir / name ) if hasattr (target_dir , "with_segments" ) \
1312+ parent = target_dir if hasattr (target_dir , "with_segments" ) \
13131313 else self .with_segments (target_dir , name )
13141314
1315- if self .info .is_file ():
1316- if not exist_ok and target .info .is_file ():
1317- raise FileExistsError (EEXIST , "File exists" , str (target ))
1318- return self .copy (target , ** kwargs )
1315+ dest = parent / self .name
13191316
1320- if self .info .is_dir ():
1321- if target .info .exists () and target .info .is_dir ():
1322- if not exist_ok :
1323- raise FileExistsError (EEXIST , "File exists" , str (target ))
1324-
1325- for child in self .iterdir ():
1326- child .copy_into (target , exist_ok = exist_ok , ** kwargs )
1327- return target .joinpath ()
1317+ if not exist_ok and dest .exists ():
1318+ raise FileExistsError (EEXIST , "File exists" , str (dest ))
13281319
1329- return self .copy (target , ** kwargs )
1330-
1331- return self .copy (target , ** kwargs )
1320+ if self .info .is_dir ():
1321+ if not dest .exists ():
1322+ os .mkdir (dest )
1323+ elif not dest .is_dir ():
1324+ raise ValueError (f"{ dest !r} is not a directory" )
1325+ for child in self .iterdir ():
1326+ child .copy_into (dest , exist_ok = exist_ok , ** kwargs )
1327+ return dest .joinpath ()
1328+
1329+ return self .copy (dest , ** kwargs )
13321330
13331331 def _copy_from (self , source , follow_symlinks = True , preserve_metadata = False ):
13341332 """
0 commit comments