[Tutor] gzip

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Dec 2 08:28:58 CET 2004

On Thu, 2 Dec 2004, Ramkumar Parimal Alagan wrote:

> I am trying to zip some files off a folder in windows Xp, can u let me
> know is wrong in the script below.

Hi Ramkumar,

Do you get an error message?  If so, please show the error message to us,
so that we can be sure that whatever problems we see are the same problems
that you're seeing.  *grin*

> zip = gzip.open(source[rb[9[target]]])

Ok, I see that you're trying to use the gzip module that's documented in:


However, I think you're misreading the documentation.  When the
documentation says:

open(filename[, mode[, compresslevel]])

This is a shorthand for GzipFile(filename, mode, compresslevel). The
filename argument is required; mode defaults to 'rb' and compresslevel
defaults to 9.

the brace symbols '[' ']' that are used in the documentation are meant to
show optional parameter arguments.  When you are using gzip.open(), don't
put those brace sybmols literally into the statement: they're meant as
typographic hints to you.

The documentation for open() says that it can take in either one, two, or
three arguments.  The first arugment is the filename, the second is the
mode, and the third is the compression level.  So instead of:

> zip = gzip.open(source[rb[9[target]]])

you may want to try something like:

zip = gzip.open(target, "rb", 9)

If you are already familiar with Python's regular open() command, just
think of gzip.open() as a specialized version that transparently
compresses as you write to it.

> if os.system(zip) == 0:
>     print 'Successful backup to', target
> else:
>     print 'Backup FAILED'

This looks problematic.  os.system() takes in a string that's passed to
the shell for execution.  But you don't need to shell out if you're using
the 'gzip' module: the program already has a file-like object that can be
written into.

More information about the Tutor mailing list