If you have to copy subfolders too, then you need a few more lines of code:

 

import glob, os, shutil

for filename in glob.glob('/tmp/folder1/*') + glob.glob('/tmp/folder2/*'):

ááá if os.path.isdir(filename):

ááá ááááshutil.copytree(filename, os.path.join('/tmp/folder3',

ááááááááááá os.path.basename(filename)))

ááá else:

ááááááá shutil.copy(filename, '/tmp/folder3')

 

This works as long as there aren’t any subfolders or files in folder2 that have the same name a subfolder in folder1.

 

David H

 

From: Nicolai Odum [mailto:nodum@csc.com]
Sent: Thursday, May 14, 2009 11:23 AM
To: David Handy
Cc: jython-users@lists.sourceforge.net
Subject: RE: [Jython-users] Howto merge 2 folder into a new folder

 


Hey David

Thx for the reply

I forgot (sorry) to say that folder1 and folder2 contains subfolders. And if I remember correctly then copy command expect the folder to exist already.

You got a good idea on how to solve that problem (in a elegant way)

Cheers

  Nicolai Marck ědum




David Handy <David.Handy@sas.com>

14-05-2009 17:07

To

Nicolai Odum/SCA/SC/CSC@CSC, "jython-users@lists.sourceforge.net" <jython-users@lists.sourceforge.net>

cc

Subject

RE: [Jython-users] Howto merge 2 folder into a new folder

 




I like the glob module.
 
import glob
import shutil
for filename in glob.glob('/tmp/folder1/*') + glob.glob('/tmp/folder2/*'):
shutil.copy(filename, '/tmp/folder3')
 
David H
 
From: Nicolai Odum [mailto:nodum@csc.com]
Sent:
Thursday, May 14, 2009 7:23 AM
To:
jython-users@lists.sourceforge.net
Subject:
[Jython-users] Howto merge 2 folder into a new folder

 

Hello


The pledge:

Howto in a platform independent copy files from 2 folders with a unknown number of files into a new folder in jython


like (in unix)

cp /tmp/folder1/* /tmp/folder3

cp /tmp/folder2/* /tmp/folder3


The turn:

(I use shutil)

shutil.copytree('/tmp/folder1/' ,  '/tmp/folder3')

shutil.copytree('/tmp/folder2/' ,  '/tmp/folder3')


The presitge:

Well not very fancy actually - I get a exception on the second call to copytree because copytree demands that target folder doesn't exsists. (I can't understand *why* it demands that)

shutil.copy doesn't accept the input to be a folder so that can't be used either


How do I turn this into a good prestige :-D

Cheers

 Nicolai ědum