The whole process of developing a Python script
Problem: complete a program that backs up all of our important programs.
Steps:
- The files and directories to be backed up are specified by a list.
The file is backed up into a zip file.
The name of the ZIP archive is the current date and time.
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.
- 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>