Last active
January 31, 2019 15:42
-
-
Save devilgate/de73e266872d3006d2cdac3e72033894 to your computer and use it in GitHub Desktop.
Creating a temporary or backup file in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from pathlib import Path | |
def create_backup_file(file): | |
"""Create a timestamped, uniquely-named backup version of the received | |
file | |
""" | |
while True: | |
backup_file_name = "{}.{}.bak".format(file.name, | |
time.strftime("%Y-%m-%d-%H-%M-%S")) | |
backup_file = Path(file.parent) / backup_file_name | |
if not backup_file.exists(): | |
return backup_file | |
# if os.path.exists(temp_file): | |
# new_temp_file = "{}.{}.tmp".format( | |
# file, time.strftime("%Y-%m-%d-%H-%M-%S")) | |
# os.rename(temp_file, new_temp_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment