[Tutor] writing sample program that zips files and saves in a backup directory

Kent Johnson kent37 at tds.net
Tue Oct 13 20:42:35 CEST 2009

On Tue, Oct 13, 2009 at 2:01 PM, David Eric <ciik13 at gmail.com> wrote:
> doing a python tutorial and one of the assignments says to develop a program
> that backsup files to zip files into a backup directory
> im using Darwin 10.0.0 unix version:
> this is what i came up with thus far, i did copy the sample program given
> but made changes for my unix OS:
> #!/usr/bin/env python
> # Filename : backup_prog.py
> import os
> import time
> source = '/Users/davidteboul/bin/python'
> target_dir = '/Users/davidteboul/backups'
> target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.gz'
> zip_command = "gzip -qr {0} {1}".format(target, ' '.join(source))

Two problems:
- gzip operates on single files, if you want to gzip multiple files
you should tar them first. That is why it gives you the initial file
not found. From 'man gzip':
Whenever possible, each file is replaced by one with the extension
.gz, while keeping the same ownership  modes,  access  and
modification times.

- the ' '.join(source) shouldn't be there

In [25]: source = '/Users/davidteboul/bin/python'

In [26]: ' '.join(source)
Out[26]: '/ U s e r s / d a v i d t e b o u l / b i n / p y t h o n'

Here is zip_command:
'gzip -qr /Users/davidteboul/backups\\20091013143741.gz / U s e r s /
d a v i d t e b o u l / b i n / p y t h o n'

> i ran it and my computer went nuts!..its started trying to gzip every file
> on my computer...luckily it said access denied oneverything but i dont
> understand why it would zip files that i didnt even specify..is it the 'r'
> comman in the gzip command line that prompted this?

The incorrect command line combined with -r

Next time, you might want to print the command line before going live
with it ;-)


More information about the Tutor mailing list