79280159

Date: 2024-12-14 06:16:27
Score: 1
Natty:
Report link

Open the destination with x mode, which fails if the file already exists.

def safe_copy(srcfile, destfile):
    with open(srcfile, "rb") as src, open(destfile, "wxb") as dest:
        dest.write(src.read())

Opening destfile will raise FileExistsError. See What does python3 open "x" mode do?

Reasons:
  • Probably link only (1):
  • Low length (0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • High reputation (-2):
Posted by: Barmar