Hello people.
I've been sitting around reading "A Byte of Python" as many others, and am now stuck at the end of writing a simple backup program.
Now, in the book you are instructed to build a backup program, first by explicit instructions, later by being asked to rebuild the same program but without making the os.system call needed to zip the backup file. Instead, you are supposed to use a module supplied in the library called "zipfile.py".
The following is my program code:
<<<<<<<<<<<<<<<
import os
import time
import zipfile
source = ['/Users/Andrea/Documents/Code']
target_dir = '/Users/Andrea/Desktop/Backup'
today = target_dir + os.sep + time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')
comment = input('''If you want to, you may enter a comment which will then be
added to the filename.\n\nPlease note that spaces in the comment will
be replaced with underscores. You may skip this step by
pressing Return.\n\nPlease enter a comment: ''')
if len(comment) == 0:
target = today = today + os.sep + now + 'zip'
else:
target = today + os.sep + now + '_' + \
comment.replace(' ', '_') + '.zip'
if not os.path.exists(today):
os.mkdir(today)
print('Successfully created directory', today)
ZipFile = "ZipFile -qr {0 }{1}".format(target, ' '.join(source))
if zipfile.ZipFile ==0:
print('Successfully backed up to', target)
else:
print('Backup FAILED')
>>>>>>>>>>>>>>>>
The following is my error code:
>>>>>>>>>>>>>>>
Traceback (most recent call last):
File "/Users/Andrea/Documents/Code/backup_ver5.py", line 27, in <module>
ZipFile = "ZipFile -qr {0 }{1}".format(target, ' '.join(source))
KeyError: '0 '
<<<<<<<<<<<<<<<
So, my guess is that returned line doesn't work in the zipfile module imported, and I'm pretty sure that's not too far from the truth. The problem is that i _don't_ know what to do about it.
Any guesses?