Python uses 7z to decompress the software backup file script for sharing

Source: Internet
Author: User

Required installation:

1. Python
2.7z decompression software

Backup_2.py

Copy codeThe Code is as follows:
# Filename: backup_2.py

'''Backup files.
Version: V2, based on Python 3.3
Usage: backup. py-s: "dir1 | dir2 |..."-t: "target_dir" [-c: "comment"]
-S: The source directories.
-T: The target directory.
-C: Optional, any comment.
Examples:
Backup. py-s: "c: \ src \ F1 | c: \ src \ F2 | c: \ src \ F 3"-t: "c: \ backup"
Backup. py-s: "c: \ src \ F 3"-t: "c: \ backup"-c: "For sample "'''

Import OS
Import sys
Import time

# Read sys. argv
Print (sys. argv)
If len (sys. argv) <2:
Print (_ doc __)
Sys. exit ()

Source = []
Target_dir =''
Comment =''
For arg in sys. argv:
If arg. startswith ('-s :'):
Source = arg [3:]. split ('| ')
Print (source)
Elif arg. startswith ('-t :'):
Target_dir = arg [3:] + OS. sep
Print (target_dir)
Elif arg. startswith ('-c :'):
Comment = arg [3:]
Print (comment)

For I in range (0, len (source )):
Source [I] = "\" "+ source [I] + "\""
Print (source [I])

# Make the file name with the time and comment
Today = target_dir + time. strftime ('% Y % m % D ')
Now = time. strftime ('% H % M % s ')

If len (comment) = 0: # check if a comment was entered
Targetdomaintodaydomainos.sep?now='.7z'
Else:
Target = today + OS. sep + now + '_' + \
Comment. replace (','{}'.7z'

# Create the subdirectory by day
If not OS. path. exists (today ):
OS. mkdir (today) # make directory
Print ('successfully created directory', today)

# Zip command
Zip_command = "7z a % s" % (target, ''. join (source ))
Print (zip_command)

# Run the backup
If OS. system (zip_command) = 0:
Print ('successful backup to ', target)
Else:
Print ('backup failed ')

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.