Python Instances: Backup backups

Source: Internet
Author: User
Tags rar

Python Instances: Backup backups

This article comes from an example in the Concise Python tutorial

1. Ask the question: I want a program that can create backups for all of my important files.
2. Analyze clear questions: How do we determine which files to back up? Where is the backup saved? How do we store backups?
3. List of design programs:

1). The files and directories that need to be backed up are specified by a list.

2). The backup should be saved in the primary backup directory.

3). File backup into a zip file.

4) The name of the. zip archive is the current date and time.

4. Write the Code:

#Filename:backup_ver1.pyImportOSImport Time#1. The files and directories to is backed up is specified in a list.Source = R'C:\python34'#2. The backup must is stored in a main backup directoryTarget_dir = R'c:\python34\scripts' #Remember to the change of this-what do you'll be using#3. The files is backed up to a RAR file.#4. The name of the RAR archive is the current date and timetarget = Target_dir + time.strftime ('%y%m%d%h%m%s') +'. rar'#5. We use the RAR command in Windows to put the "files in a" zip archive,you must to is sure you have installed WinRAR A and that in your pathRar_command = R'"C:\Program files\winrar\winrar.exe" A%s%s-r'%(Target,source)#Run the backupifOs.system (rar_command) = =0:Print('Successful backup to'), TargetElse:    Print('Backup FAILED')

Analysis:

1. Note that the source, Target_dir address can be arbitrarily specified. Source is pointing to a file that needs to be backed up, and target_dir points to the address that needs to be saved.

Source = R'e:\code'= R'e:\code'

2. The name of the ZIP archive compressed document is specified with Target, which uses the

The addition operator is a concatenated string (that is, two strings are concatenated together to return a new string); Time.strftime () returns the current time; '. rar ' extension;

The string join method converts the source list to a string. Source can be replaced with ". Join (source), seemingly only use this", inside cannot add other symbols.

Rar_command or Zip_command, must have the software to use the system,

You must put the WinRAR path inside your environment variable before you can use the WinRAR command line directly. or you want to add the WinRAR installation path like this:  

Rar_command = "Zip-qr '%s '%s" % (Target,source).

The zip command has some options and parameters. The-Q option is used to indicate that the zip command works quietly. The-r option indicates that the ZIP command works recursively on the directory, that is, it includes subdirectories and files in subdirectories. Two options can be combined into an abbreviated form-QR. So check the relevant help documentation yourself.



Python instance: Backup backups

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.