Zip command error

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.

Please note that spaces in the comment will
be replaced with underscores. You may skip this step by
pressing Return.

Please 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
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?

Comments

  • Hello there!

    I'm assuming you are trying to write the program usign zipfile module. I must confess that I can`t tacke it. I did it, but using tarfile not zip file. Tomorrow I will try to crack this problem with zipfile.

    If you want to try to do this with tarfile, here is some code that may help significatly:

    [code]
    tar = tarfile.open("test.tar.bz2", "w:bz2")

    for name in ["file1.txt", "file2.txt", "file3.txt"]:
    tar.add(name)
    tar.close()
    [/code]
    code was found [link=http://www.daniweb.com/code/snippet630.html#]here[/link]

    So.. Good luck :)
  • Done. It works. I decided to show you source, not just to give a clue, as descriptions of zipfile and tarfile seem to me very hard to understand when you are a newbie, like I am.


    [code]
    #!/usr/bin/env python3
    # Filename: backup5.py

    #This scrip archives fixed locations, and put them in one directory named by date
    #and also name archives by current time.


    #modules which will be used
    import zipfile
    import time
    import os

    #what is going to be zipped
    source = '/home/user/Docs'

    #where the script is going to put it
    destination = '/home/user/Desktop'

    #names which will be needed later
    dir_name = destination + os.sep + time.strftime('%Y%m%d')
    file_name = dir_name + os.sep + time.strftime('%H%M%S') + '.zip'


    #create a directory
    if os.path.exists(dir_name) == 0: #checks if the directory exists
    os.mkdir(dir_name) #creates one if not
    print('Directory', dir_name, 'successfully created')

    #comment
    comment = input('Please enter a comment ->')
    if len(comment)!= 0: #if there is any comment script will add it to the file name
    file_name = dir_name + os.sep + time.strftime('%H%M%S') + '_' + comment.replace(' ','_') + '.zip'
    print('Your comment has beed successfully added.')


    #make a zip file
    #create an backup object, 'w' means write as we
    #want to write stuff into this file. os.listdir
    #as name indicates make a list of files we want
    #to zip. backup.close is an end of zip process.

    backup = zipfile.ZipFile(file_name, 'w', zipfile.ZIP_DEFLATED)
    for name in os.listdir(source):
    backup.write(source + os.sep + name)
    backup.close()

    #end of script
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion