From: David H. <Dav...@sa...> - 2009-05-14 15:45:09
|
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:no...@cs...] Sent: Thursday, May 14, 2009 11:23 AM To: David Handy Cc: jyt...@li... 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 <Dav...@sa...> 14-05-2009 17:07 To Nicolai Odum/SCA/SC/CSC@CSC, "jyt...@li..." <jyt...@li...> 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:no...@cs...] Sent: Thursday, May 14, 2009 7:23 AM To: jyt...@li... 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 |