Create a backup file :param src: path of source file to create backup :param suffix: suffix of backup file :return: the path of backup file
(src: pathlib.Path, suffix: str)
| 23 | |
| 24 | |
| 25 | def create_backup(src: pathlib.Path, suffix: str) -> pathlib.Path: |
| 26 | """Create a backup file |
| 27 | |
| 28 | :param src: path of source file to create backup |
| 29 | :param suffix: suffix of backup file |
| 30 | :return: the path of backup file |
| 31 | """ |
| 32 | conflicts = 0 |
| 33 | while True: |
| 34 | backup_path: pathlib.Path = src.with_name(src.name + suffix) |
| 35 | if conflicts > 0: |
| 36 | backup_path = backup_path.with_name(f"{backup_path.name}.{conflicts}") |
| 37 | if backup_path.exists(): |
| 38 | conflicts += 1 |
| 39 | continue |
| 40 | shutil.copy2(src, backup_path) |
| 41 | return backup_path |
| 42 | |
| 43 | |
| 44 | def file_tree_iterator( |