• No se han encontrado resultados

l. En relación con las actividades de uso público

The second version works fine when I do many backups, but when there are lots of backups, I am finding it hard to differentiate what the backups were for! For example, I might have made some major changes to a program or presentation, then I want to associate what those changes are with the name of the zip archive. This can be easily achieved by attaching a user-supplied comment to the name of the zip archive.

Note

The following program does not work, so do not be alarmed, please follow along because there's a lesson in here.

#!/usr/bin/python

# Filename: backup_ver3.py

import os

import time

# 1. The files and directories to be backed up are specified in a list.

source = ['"C:\\My Documents"', 'C:\\Code']

# Notice we had to use double quotes inside the string for names with

spaces in it.

# 2. The backup must be stored in a main backup directory

target_dir = 'E:\\Backup' # Remember to change this to what you will be

using

# 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 = target_dir + os.sep + time.strftime('%Y%m%d')

Python en:Problem Solving 76

now = time.strftime('%H%M%S')

# Take a comment from the user to create the name of the zip file

comment = input('Enter a comment --> ')

if len(comment) == 0: # check if a comment was entered

target = today + os.sep + now + '.zip'

else:

target = today + os.sep + now + '_' +

comment.replace(' ', '_') + '.zip'

# Create the subdirectory if it isn't already there

if not os.path.exists(today):

os.mkdir(today) # make directory

print('Successfully created directory', today)

# 5. We use the zip command to put the files in a zip archive

zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))

# Run the backup

if os.system(zip_command) == 0:

print('Successful backup to', target)

else:

print('Backup FAILED')

Output:

$ python backup_ver3.py

File "backup_ver3.py", line 25

target = today + os.sep + now + '_' + ^ SyntaxError: invalid syntax

How This (does not) Work:

This program does not work! Python says there is a syntax error which means that the

script does not satisfy the structure that Python expects to see. When we observe the error given by Python, it also tells us the place where it detected the error as well. So we start

debugging our program from that line.

On careful observation, we see that the single logical line has been split into two physical lines but we have not specified that these two physical lines belong together. Basically, Python has found the addition operator (+) without any operand in that logical line and hence it doesn't know how to continue. Remember that we can specify that the logical line continues in the next physical line by the use of a backslash at the end of the physical line. So, we make this correction to our program. This correction of the program when we find errors is called bug fixing.

Python en:Problem Solving 77

Fourth Version

#!/usr/bin/python # Filename: backup_ver4.py import os import time

# 1. The files and directories to be backed up are specified in a list.

source = ['"C:\\My Documents"', 'C:\\Code']

# Notice we had to use double quotes inside the string for names with

spaces in it.

# 2. The backup must be stored in a main backup directory

target_dir = 'E:\\Backup' # Remember to change this to what you will be

using

# 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 = target_dir + os.sep + time.strftime('%Y%m%d')

# The current time is the name of the zip archive

now = time.strftime('%H%M%S')

# Take a comment from the user to create the name of the zip file

comment = input('Enter a comment --> ')

if len(comment) == 0: # check if a comment was entered

target = today + os.sep + now + '.zip'

else:

target = today + os.sep + now + '_' + \ comment.replace(' ', '_') + '.zip'

# Create the subdirectory if it isn't already there

if not os.path.exists(today):

os.mkdir(today) # make directory

print('Successfully created directory', today)

# 5. We use the zip command to put the files in a zip archive

zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))

# Run the backup

if os.system(zip_command) == 0:

print('Successful backup to', target)

else:

print('Backup FAILED')

Python en:Problem Solving 78

$ python backup_ver4.py

Enter a comment --> added new examples

Successful backup to E:\Backup\20080702\202836_added_new_examples.zip

$ python backup_ver4.py Enter a comment -->

Successful backup to E:\Backup\20080702\202839.zip

How It Works:

This program now works! Let us go through the actual enhancements that we had made in version 3. We take in the user's comments using the input function and then check if the user actually entered something by finding out the length of the input using the len

function. If the user has just pressed enter without entering anything (maybe it was just a routine backup or no special changes were made), then we proceed as we have done before.

However, if a comment was supplied, then this is attached to the name of the zip archive just before the .zip extension. Notice that we are replacing spaces in the comment with underscores - this is because managing filenames without spaces are much easier.

Documento similar