Created
January 22, 2018 07:49
-
-
Save William-Hill/823617a31966623548dd18bfda230910 to your computer and use it in GitHub Desktop.
Backup a 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
'''A simple function to create a backup of a file with a date stamp and backup extension''' | |
import datetime | |
import shutil | |
import os | |
def create_backup_file(file_name, backup_extension=".bak", date=str(datetime.date.today())): | |
'''Create a backup of a file using the given backup extension''' | |
backup_file_name = os.path.join(file_name, backup_extension) | |
try: | |
shutil.copyfile(file_name, backup_file_name) | |
os.chmod(backup_file_name, 600) | |
except OSError: | |
print("Could not create backup file: %s\n", backup_file_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment