recursively archiving files

Larry Bates larry.bates at websafe.com
Mon Apr 9 17:45:46 EDT 2007


bahoo wrote:
> Hi,
> 
> Can I use python to recursively compress files under subdirectories
> with a certain format such as "ABC_XXX_XXX.dat" into a .gz or .zip
> file?  I used to do it with "tar" on unix, but I don't like to put
> commands into a single line, as it is often more prone to error.
> 
> Thanks
> bahoo
> 
Use os.walk to talk your branch of directories.
Use glob.glob to find only those files that match your mask.
Use zipfile or tarfile module (depending on if you want .gz or
.zip compressed file) to add each file to the compressed file.

-Larry



More information about the Python-list mailing list