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

Dave Angel davea at ieee.org
Tue Oct 13 22:14:31 CEST 2009


(You top-posted, instead of adding your part to the end.  That's frowned 
upon in a mailing list, at least in this one.)
(You replied privately, and forgot to CC the tutor list.  I'm forwarding 
it there with my response)

David Eric wrote:
> wow thank you
> yes...youre right i did want to pass a list of strings
> im copying the sample program and adding my own directories, i missed what
> the example was trying to show in that instance.
> as far as the recursive option, you mean the -r would process all
> subdirectories under /Users?
> im sorry for being so ignorant on this but what would be an example of an
> innoucuous script?
>
>
>
> On Tue, Oct 13, 2009 at 3:23 PM, Dave Angel <davea at ieee.org> wrote:
>
>   
>> David Eric 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))
>>>
>>> if os.system(zip_command) == 0:
>>>    print('Successful backup to', target)
>>> else: print('Backup FAILED')
>>>
>>>
>>>
>>>
>>>       
>> When launching external programs you're not familiar with, you really need
>> to do a dry-run test, just to be sure.  It wouldn't have been hard to come
>> up with something that trashes your system.  (Ask me, I issued a Unix
>> command once that was deleting all files in a valuable tree, and the only
>> thing that saved my bacon was that Unix file I/O was so slow.  It only did
>> about two files per second, so I was able to Ctrl-C after only losing about
>> 5 files, all of which were quickly recoverable.  The next directory down
>> would have been a disaster, however, and had it completed, we would have had
>> to restore from the previous night's nightly backup, effectively wasting the
>> day for most employees.)
>>
>>
>> I see two problems, but since I don't know gzip, there may very well be
>> more.
>>
>> The -r switch on nearly all Unix utilities means "recurse" which means to
>> process all subdirectories of the starting location.
>>
>> The ' '.join(source) takes your source string, and puts spaces as every
>> second character.  The only thing I can think of you might have meant was to
>> pass it a list of strings.  So if source were
>>  source = ['/Users/davidteboul/bin/python', '/Users/davidteboul/bin/perl']
>>
>> then it'd make sense.
>>
>> Anyway, I'd launch some innocuous script, that echoes its arguments, rather
>> than trying it directly on gzip.   And of course, I'd look at the man page
>> for gzip, to see just what its arguments are supposed to look like.
>>
>> DaveA
>>
>>
>>     
I haven't used Unix in over 15 years, so I don't remember.  But I think 
it had something like 'echo' that simply echoed its arguments to 
stdout.  In Windows, I'd make a one-line batch file like:

Echo Running %0 script, with parameters %*

or

Echo Running %0 script, with parameters %1, %2, %3, %4, %5

The script could also display things like the current directory, maybe 
some environment variables.  Whatever might be relevant to help see 
what's going on.

The -r switch should mean to process all files in  
/Users/davidteboul/bin/python, plus all files in any of its 
subdirectories. Not starting at /Users, just starting in that 
...../python directory.

HTH
DaveA





More information about the Tutor mailing list