Python

Moderators: None (Apply to moderate this forum)
Number of threads: 473
Number of posts: 1172

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Zip command error Posted by Sargaroth on 16 Jul 2009 at 8:13 AM
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?
Report
Re: Zip command error Posted by Gibs on 18 Jul 2009 at 6:10 PM
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:

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

for name in ["file1.txt", "file2.txt", "file3.txt"]:
    tar.add(name)
tar.close()

code was found here

So.. Good luck :)
Report
Re: Zip command error Posted by Gibs on 19 Jul 2009 at 4:46 AM
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.


#!/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




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.