看copytree源码,核心就是这个循环:
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
copy2(srcname, dstname)
# 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)))
如果您yield
在最后添加一个权利,您的循环将起作用 - 但您将在复制每个文件或目录后打印,而不是按时间间隔打印(您time.sleep
会在副本之间发生,只是让整个事情花费更长的时间;因为每个时间间隔,是的,您将需要线程)。但是,如果您愿意,这也将允许您提供更详细的反馈 - 例如,您可以yield name
(或,yield (srcname, destname)
)能够打印有关已复制文件的反馈。