[Python-Dev] A smarter shutil.copytree ?
Guido van Rossum
guido at python.org
Thu Apr 17 19:06:17 CEST 2008
Sounds like a neat little feature. Looking forward to it. Maybe the
most useful use case would be to provide glob-style patterns for
skipping files or directories (and their contents).
--Guido
On Thu, Apr 17, 2008 at 9:52 AM, Tarek Ziadé <ziade.tarek at gmail.com> wrote:
> Hi,
>
> shutil.copytree is very convenient to make recursive copies, but
> os.walk has to be used everytime some filtering
> has to be done on the files copied., if you want to avoid copying some files.
>
> The code pattern with os.walk is pretty talkative :
>
> --------------------- copying a source folder to a target folder, but
> the pyc/pyo files
> os.mkdir(target)
> for root, dirs, filenames in os.walk(source):
> root_target = root.replace(source, target)
> for dir_ in dirs:
> target_dir = join(root_target, dir_)
> if os.path.exists(target_dir):
> continue
> os.mkdir(target_dir)
> for filename in filenames:
> filename_source = join(root, filename)
> filename_target = join(root_target, filename)
> if (os.path.exists(filename_target) or
> os.path.splitext(filename) in ('.pyc', '.pyo')):
> continue
> shutil.copyfile(filename_source, filename_target)
> --------------------
>
> If we could provide a callable argument to shutil.copytree, this would
> allow simplifying a lot the code:
>
> --------------------- copying a source to a target, but the pyc/pyo file
> def filtering(source, target):
> return os.path.splitext(filename) not in ('.pyc', '.pyo')
>
> shutil.copytree(source, target, filter_=filtering)
> ---------------------
>
> This is a very current pattern in my code, and I think this could be
> an interesting enhancement to
> shutil. So if people think it is a good idea, I can work on a patch
> and submit it to the tracker.
>
> Regards
>
> Tarek
>
> --
> Tarek Ziadé | Association AfPy | www.afpy.org
> Blog FR | http://programmation-python.org
> Blog EN | http://tarekziade.wordpress.com/
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
>
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list