Copy directory tree without copying files

Jeremy Conlin jlconlin at gmail.com
Wed Sep 23 17:43:27 CEST 2009


On Sep 23, 9:31 am, Tim Golden <m... at timgolden.me.uk> wrote:
> Jeremy Conlin wrote:
> > On Sep 23, 9:15 am, Tim Golden <m... at timgolden.me.uk> wrote:
> >> Jeremy Conlin wrote:
> >>> I am trying to copy a folder hierarchy from one location to another.
> >>> I can use the shutil.copytree function to copy the folder tree, but I
> >>> don't want the files copied, just the folders.  What is a good way to
> >>> approach this?
> >>> Thanks,
> >>> Jeremy
> >> Use os.walk and create the directories as they appear?
>
> >> TJG
>
> > I was hoping to use os.walk, but this doesn't work because I wont know
> > when os.walk descends into a new directory or which directory it
> > descends into.  Perhaps I don't understand everything about it.
>
> Some rough but working code might help you out here:
>
> <code>
> import os
>
> source_dir = "c:/temp/oldtree"
> target_dir = "c:/temp/newtree" # should not exist yet
>
> for dirpath, dirnames, filenames in os.walk (source_dir):
>   os.mkdir (os.path.join (target_dir, dirpath[1+len (source_dir):]))
>
> os.startfile (target_dir)
>
> </code>
>
> TJG

Perfect.  That's exactly what I needed.
Jeremy



More information about the Python-list mailing list