ivapopova

SimpleBackup - Solution

Apr 13th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. import os
  2. import datetime
  3.  
  4. def backup(src, dest, timestamp):
  5.   """Backup files in src folder into dest folder.
  6.      Do not remove the files in source folder.
  7.      To each file attach suffix with current timestamp in the format '%Y-%m-%d_%H_%M_%S%'
  8.  
  9.    Args:
  10.        src (string): Source folder
  11.        dest (string): Destination folder
  12.  
  13.    Example:
  14.      src/track5.mp3 => dest/track5.mp3_2018-04-12_18-30-45
  15.    """
  16.  
  17.   # get list of all file/dir names in source folder
  18.   entries = os.listdir(src)
  19.  
  20.   for filename in entries:
  21.     src_full_filename = src+filename
  22.     dest_full_filename = dest+filename+"_"+timestamp
  23.     os.rename(src_full_filename, dest_full_filename)
  24.  
  25.  
  26. def get_timestamp():
  27.   #get the current local date-time
  28.   cldt = datetime.datetime.today()
  29.  
  30.   # get the timestamp as a string with given format
  31.   timestamp = datetime.datetime.strftime(cldt, '%Y-%m-%d_%H_%M_%S')
  32.  
  33.   return timestamp
  34.  
  35.  
  36. ###########################################################
  37. # MAIN
  38. ###########################################################
  39. # set the source and destination paths for the backup
  40. # note, the trailing foreword slash is important
  41. src = "../data/"
  42. dest = "./"
  43.  
  44. timestamp = get_timestamp()
  45. print(timestamp)
  46.  
  47. backup(src, dest, timestamp)
Advertisement
Add Comment
Please, Sign In to add comment