Copy directory tree without copying files

J

Jeremy Conlin

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
 
T

Tim Golden

Jeremy said:
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
 
J

Jeremy Conlin

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.

Thanks,
Jeremy
 
P

Peter Otten

Jeremy said:
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?

The easiest is

def ignore(folder, names):
return set(n for n in names if not
os.path.isdir(os.path.join(folder, n)))

shutil.copytree(source, dest, ignore=ignore)

Peter
 
J

Jeremy Conlin

Have you considered using os.walk?http://docs.python.org/library/os.html#os.walk

It won't be completely automated, but I think it should allow you to
easily walk the directory structure to reproduce it in another
location.

If the file names/extensions are predictable you might also be able to
use the ignore attribute in the shutil.copytree function to prevent
copying files:http://docs.python.org/library/shutil.html#shutil.copytree

Daniel

Thanks, that helps. I could write a function like

def ignore_files(dirpath):
ignore = []
for f in os.listdir(dirpath)
if os.path.isfile(f):
ignore.append(f)
return ignore


That seems like it will work (haven't tested yet). Thanks for the
help.
Jeremy
 
T

Tim Golden

Jeremy said:
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
 
J

Jeremy Conlin

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
 
G

Grant Edwards

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?

Just in case there's no real requirement to use Python...

$ (cd srcdir; find . -type d) | (cd dstdir; xargs mkdir)
 
J

Jeremy Conlin

Just in case there's no real requirement to use Python...

$ (cd srcdir; find . -type d) | (cd dstdir; xargs mkdir)

Thanks, but this has to be platform independent so Python seemed like
a good choice.

Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top