Copy data and metadata. Return the file's destination. Metadata is copied with copystat(). Please see the copystat function for more information. The destination may be a directory. If follow_symlinks is false, symlinks won't be followed. This resembles GNU's "cp -P src dst".
(src, dst, *, follow_symlinks=True)
| 491 | return dst |
| 492 | |
| 493 | def copy2(src, dst, *, follow_symlinks=True): |
| 494 | """Copy data and metadata. Return the file's destination. |
| 495 | |
| 496 | Metadata is copied with copystat(). Please see the copystat function |
| 497 | for more information. |
| 498 | |
| 499 | The destination may be a directory. |
| 500 | |
| 501 | If follow_symlinks is false, symlinks won't be followed. This |
| 502 | resembles GNU's "cp -P src dst". |
| 503 | """ |
| 504 | if os.path.isdir(dst): |
| 505 | dst = os.path.join(dst, os.path.basename(src)) |
| 506 | |
| 507 | if hasattr(_winapi, "CopyFile2"): |
| 508 | src_ = os.fsdecode(src) |
| 509 | dst_ = os.fsdecode(dst) |
| 510 | flags = _winapi.COPY_FILE_ALLOW_DECRYPTED_DESTINATION # for compat |
| 511 | if not follow_symlinks: |
| 512 | flags |= _winapi.COPY_FILE_COPY_SYMLINK |
| 513 | try: |
| 514 | _winapi.CopyFile2(src_, dst_, flags) |
| 515 | return dst |
| 516 | except OSError as exc: |
| 517 | if (exc.winerror == _winapi.ERROR_PRIVILEGE_NOT_HELD |
| 518 | and not follow_symlinks): |
| 519 | # Likely encountered a symlink we aren't allowed to create. |
| 520 | # Fall back on the old code |
| 521 | pass |
| 522 | elif exc.winerror == _winapi.ERROR_ACCESS_DENIED: |
| 523 | # Possibly encountered a hidden or readonly file we can't |
| 524 | # overwrite. Fall back on old code |
| 525 | pass |
| 526 | else: |
| 527 | raise |
| 528 | |
| 529 | copyfile(src, dst, follow_symlinks=follow_symlinks) |
| 530 | copystat(src, dst, follow_symlinks=follow_symlinks) |
| 531 | return dst |
| 532 | |
| 533 | def ignore_patterns(*patterns): |
| 534 | """Function that can be used as copytree() ignore parameter. |