Skip to content

Instantly share code, notes, and snippets.

@devilgate
Last active January 31, 2019 15:42
Show Gist options
  • Save devilgate/de73e266872d3006d2cdac3e72033894 to your computer and use it in GitHub Desktop.
Save devilgate/de73e266872d3006d2cdac3e72033894 to your computer and use it in GitHub Desktop.
Creating a temporary or backup file in Python
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