Sie können Ihre eigene sehr-leicht-modifizierte Version von copytree schreiben, die helfen werden:
import shutil
import os.path
import os
def file_copied():
print "File copied!"
# Usage example: copytree(src, dst, cb=file_copied)
def copytree(src, dst, symlinks=False, ignore=None, cb=None):
"""Recursively copy a directory tree using copy2().
The destination directory must not already exist.
If exception(s) occur, an Error is raised with a list of reasons.
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.
The optional ignore argument is a callable. If given, it
is called with the `src` parameter, which is the directory
being visited by copytree(), and `names` which is the list of
`src` contents, as returned by os.listdir():
callable(src, names) -> ignored_names
Since copytree() is called recursively, the callable will be
called once for each directory that is copied. It returns a
list of names relative to the `src` directory that should
not be copied.
XXX Consider this example code rather than the ultimate tool.
"""
names = os.listdir(src)
if ignore is not None:
ignored_names = ignore(src, names)
else:
ignored_names = set()
os.makedirs(dst)
errors = []
for name in names:
if name in ignored_names:
continue
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, ignore)
else:
# Will raise a SpecialFileError for unsupported file types
shutil.copy2(srcname, dstname)
if cb is not None:
cb()
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error, err:
errors.extend(err.args[0])
except EnvironmentError, why:
errors.append((srcname, dstname, str(why)))
try:
shutil.copystat(src, dst)
except OSError, why:
if WindowsError is not None and isinstance(why, WindowsError):
# Copying file access times may fail on Windows
pass
else:
errors.extend((src, dst, str(why)))
if errors:
raise Error, errors
All dies Code funktioniert anders als das Original copytree ist, einen Anruf zu einer bestimmten Funktion, nachdem sie kopiert eine machen Datei. Also in Ihrem Fall anstelle von file_copied() können Sie es aktualisieren() den Dialog, oder aktualisieren Sie eine Anzahl der Anzahl der erfolgreich kopierten Dateien, etc.
danke .. aber ein Problem .. wenn ich meine Post copytree (source, destination) Syntax an der Stelle von "# Einige Dinge passieren" Der Fortschrittsbalken beginnt nach Abschluss der Kopie des Ordners und seines Inhalts ... Erläutern Sie bitte, wie Sie sowohl copytree (Quelle, Ziel) als auch Fortschrittsbalken integrieren ... dh kopieren Sie den Ordner und seinen Inhalt, gleichzeitig sollte der Fortschrittsbalken auch den Fortschritt anzeigen. –
Wenn Sie [shutil.copytree] (http://docs.python.org/library/shutil.html#module-shutil) verwenden, sieht es so aus, als könnten Sie eine Kopierfunktion angeben (standardmäßig copy2). Eine Möglichkeit besteht darin, eine eigene Kopierfunktion mit einem Rückruf anzugeben, der meldet, wenn eine Datei kopiert wurde, wie viele Bytes kopiert wurden. Oder Sie verwenden [threads] (http://docs.python.org/library/). threading.html # module-threading) um in einen Thread zu kopieren und # Dateien/Bytes im Ziel zu überwachen. – ChrisC
Können Sie die Codierung mit einer copy2 Rückruffunktion, die den Fortschrittsbalken aktualisiert, notieren, während Sie einen Ordner und seinen Inhalt kopieren ..? Ich bin kein Experte in Python .. –