Python notes (7)

Source: Internet
Author: User

The whole process of developing a Python script

Problem: complete a program that backs up all of our important programs.

Steps:

  1. The files and directories to be backed up are specified by a list.
  2. The file is backed up into a zip file.

  3. The name of the ZIP archive is the current date and time.

  4. We use standardZipCommand, which is usually provided with the Linux/Unix release by default. Windows users can use the Info-zip program. Note that you can use any archive Command, as long as it has a command line interface, then we can pass parameters to it from our script.

  5. The backup should be saved in the master Backup Directory.

#! /Usr/bin/python <br/> # filename: backup_ver1.py </P> <p> Import OS <br/> Import time </P> <p> #1. the files and directories to be backed up are specified in a list. <br/> source = ['/home/swaroop/byte', '/home/swaroop/bin'] <br/> # If you are using Windows, use source = [R 'C: \ documents', r 'd: \ work'] or something like that </P> <p> #2. the backup must be stored in a main Backup Directory <br/> target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using </P> <p> #3. the files are backed up into a zip file. <br/> #4. the name of the ZIP archive is the current date and time <br/> Target = target_dir + time. strftime ('% Y % m % d % H % m % s') + '.zip' </P> <p> #5. we use the zip command (in Unix/Linux) to put the files in a ZIP Archive <br/> zip_command = "Zip-QR '% s' % s" % (target, ''. join (source) </P> <p> # Run the backup <br/> If OS. system (zip_command) = 0: <br/> Print 'successful backup to ', target <br/> else: <br/> Print 'backup failed' </P> <p>

Output:

$ Python backup_ver1.py <br/> successful backup to/mnt/e/backup/20041208073244.zip </P> <p>

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.