Servus,
ich weiss der Thread ist schon älter. Jedoch hab ich nach einer ähnlichen Funktionalität gesucht (brauche nur die Dateien von einem Verzeichniss zu einem anderem kopieren, nicht dessen Unterordner) und im Forum dazu nichts direkt gefunden.
Der Tip, man solle einfach in shutil.py reinschauen wie es da gemacht wird hat mir dann letztlich geholfen. Deswegen will ich mal eine modifizierte copytree Funktion posten, welche auch in vorhande Zielverzeichnisse kopieren kann.
So aber erstmal schauen wir uns die orginal copytree Funktion von shutil.py (Python 2.5.1) an:
Code: Alles auswählen
def copytree(src, dst, symlinks=0):
"""Recursively copy a directory tree using copy2().
The destination directory must not already exist.
Error are reported to standard output.
If the optional symlinks flag is true, symbolic links in the
source tree result in symbolic links in the destination tree; if
it is false, the contents of the files pointed to by symbolic
links are copied.
XXX Consider this example code rather than the ultimate tool.
"""
names = os.listdir(src)
os.mkdir(dst)
for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
copytree(srcname, dstname, symlinks)
else:
copy2(srcname, dstname)
# XXX What about devices, sockets etc.?
except (IOError, os.error), why:
print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why))
Der große Harken an der ganzen Methode ist eigentlich nur "os.mkdir(dst)", denn wenn das Zielverzeichniss vorhanden ist wirft mkdir eine Exception und Schluss ist. Das ganze kann jedoch dadruch umgangen werden das man nur mkdir aufruft wenn das Verzeichniss nicht vorhanden ist. Das sollte dann in etwa so aussehen:
So jetzt noch die if Schleife in die Funktion eingebettet und schon haben wir das gewünschte Verhalten, das die Funktion auch funktioniert, wenn das Verzeichniss schon vorhanden ist.
Die neue Funktion sieht dann foldendermaßen aus:
Code: Alles auswählen
import os
import shutil
def copytree(src, dst, symlinks=0):
"""Recursively copy a directory tree using copy2().
The destination directory must not already exist.
Error are reported to standard output.
If the optional symlinks flag is true, symbolic links in the
source tree result in symbolic links in the destination tree; if
it is false, the contents of the files pointed to by symbolic
links are copied.
XXX Consider this example code rather than the ultimate tool.
"""
names = os.listdir(src)
if not os.path.isdir(dst):
os.mkdir(dst)
for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
shutil.copytree(srcname, dstname, symlinks)
else:
shutil.copy2(srcname, dstname)
# XXX What about devices, sockets etc.?
except (IOError, os.error), why:
print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why))
Wichtig: Die Funktion wurde als neue Funktion in einem eignem Modul realisiert, somit ist auch ein "shutil" vor "copytree" bzw. "copy2" nötig. Ich würde auch abraten die shutil.py direkt zu modifizieren denn das könnte zu unerwünschten Nebeneffekten führen.
Zum Schluss will ich noch meine sehr einfache Version posten, die ich benützte um nur die Dateien und nicht die Unterverzeichnisse mit zu kopieren (auch die symbolischen Links wurden aussen vorgelassen).
Code: Alles auswählen
def copyFilesFromDir(sourceDirName, targetDirName):
try:
names = os.listdir(sourceDirName)
if not os.path.isdir(targetDirName):
os.mkdir(targetDirName)
for name in names:
srcname = os.path.join(sourceDirName, name)
dstname = os.path.join(targetDirName, name)
shutil.copy2(srcname, dstname)
except Exception, err:
print err
Als kleine Anmerkung, eigentlich arbeite ich mit Python 2.0 da ich Jython 2.1 (?) als Skriptsprache in QF-Test 2.2.1 verwende. Somit kann das "not" eventuell gegen ein "!" ausgetauscht werden, das exception Handling in meiner Methode verbesseret werden und dirverse andere Kleinigkeiten sind auch nicht ausgeschlossen.
Getest wurde alles mit Jython 2.1 und 2.2.1.
Hoffe das wird den einem oder anderem vielleicht helfen.
Gruß
Baracke