Recursively Backup Directories

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Apr 5 20:30:06 EDT 2008


En Sat, 05 Apr 2008 20:56:31 -0300, <misceverything at gmail.com> escribió:

> I am writing a script that will backup specified folders from one hard
> drive to another (for example, backup source "C:\DATA", destination "D:
> \Backup"), and was thinking of using shutil.  What I would like to do
> is recursively backup the specified directories (which copytree will
> do), but be able to specify exclusion directories (which copytree does
> not appear to allow you to do).  My initial thoughts were I'll
> probably have to use os.path.walk for the backup source directory, but
> I'm not sure how to go from there.  Thanks in advance.

I'd use os.walk (not os.path.walk) and shutil.copy2; use os.makedirs to  
create the target directory (only when it doesn't already exist).
If you remove directories from the dirnames list, they're not recursed  
into.

-- 
Gabriel Genellina




More information about the Python-list mailing list