Skip to content

Commit d1ab7a5

Browse files
committed
fix: fix review comment
1 parent 6db5926 commit d1ab7a5

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

Doc/library/pathlib.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,9 @@ Copying, moving and deleting
16041604

16051605
.. versionadded:: 3.14
16061606

1607+
.. versionchanged:: 3.15
1608+
Added exist_ok argument to allow copy duplicate directory
1609+
16071610

16081611
.. method:: Path.rename(target)
16091612

Lib/pathlib/__init__.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,19 +1309,23 @@ 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-
parent = target_dir if hasattr(target_dir, "with_segments") \
1313-
else self.with_segments(target_dir, name)
1312+
parent = target_dir if hasattr(target_dir, "with_segments") else self.with_segments(target_dir)
13141313

1315-
dest = parent / self.name
1314+
is_dir = getattr(parent, "is_dir", None)
1315+
if callable(is_dir) and not is_dir():
1316+
raise ValueError(f"{parent!r} is not a directory")
1317+
1318+
dest = parent / name
13161319

13171320
if not exist_ok and dest.exists():
13181321
raise FileExistsError(EEXIST, "File exists", str(dest))
13191322

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")
1323+
if self.is_dir():
1324+
if dest.exists():
1325+
if not dest.is_dir():
1326+
raise ValueError(f"{dest!r} is not a directory")
1327+
else:
1328+
dest.mkdir()
13251329
for child in self.iterdir():
13261330
child.copy_into(dest, exist_ok=exist_ok, **kwargs)
13271331
return dest.joinpath()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
copy_into add exist_ok allow overwrite same directory.
1+
Add ``exist_ok`` parameter to :meth:`!pathlib.Path.copy_into`.

0 commit comments

Comments
 (0)