使用7-zip備份我的桌面(版本三)

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Filename: ddup.py
from os   import sep, mkdir, path, mkdir, system
from time import strftime
def main():
    # 1. The files and directories to be backed up are specified in a list/string.
    src_dir = r'C:\Users\SY\Desktop'
    # 2. The backup must be stored in a main backup directory.
    dest_dir = r'E:\SY\DesktopBackup'
    # 3. The files are backed up into a zip file.
    # 4. The current day is the name of the subdirectory in the main directory.
    today = dest_dir + sep + strftime('%Y%m%d')
    # Create the subdirectory if it isn't already there.
    if not path.exists(today):
        mkdir(today) # Make directory.
    # The current time is the name of the zip archive.
    now = strftime('%H%M%S')
    # Take a comment from the user to create the name of the zip file.
    comment = input('Enter a comment --> ')
    comment = comment.strip()
    if len(comment) == 0: # Check if a comment was entered.
        target_file = today + sep + now + '.zip'
    else:
        target_file = today + sep + now + '_' + comment.replace(' ', '_') + '.zip'
    # 5. We use the zip command (in Windows) to put the files in a zip archive.
    zip_exe = r'D:\程序\7-Zip\7z.exe'
    zip_command = zip_exe + ' a %s %s' % (target_file, src_dir)
    if system(zip_command) == 0:
        print('Successful backup to %s.' % target_file)
    else:
        print('Backup FAILED.')
    input('Press any key to exit.')
if __name__ == '__main__':
    main()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章