Python - 记录一次用 shutil 复制目录的囧

2020-01-30  本文已影响0人  不忘初心2017

背景

做工具的时候,遇到这种需求:将目录 A 复制到目录 B。

需求简单,但可能有多种情况:

比如,我做的时候遇到一个问题, 我的目录 A 中含有隐藏的 .git 目录,结果我忘记了,直接复制覆盖了目录 B 中的 .git, 然后 git 提交后发现把 A 指向的仓库给改掉了。这个可囧了。

所以在做之前,先分析一下文件结构很重要

分析文件结构

如前所述,我的目录中有.git目录需要过滤掉。其他都还好。

探索

找 python 中有哪些模块可以复制目录。

distutils.dir_util copy_tree

from distutils.dir_util import copy_tree

python 自带,用起来也很方便,

copy_tree( src, dst[preserve_mode=1, preserve_times=1, preserve_symlinks=0, update=0, verbose=0, dry_run=0])
every file in src is copied to dst, and directories under src are recursively copied to dst. Return the list of files that were copied or might have been copied, using their output name.

然而,无法过滤指定的目录或文件。

shutil

shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)

Recursively copy an entire directory tree rooted at src, returning the destination directory. The destination directory, named by dst, **must not already exist**; 

分析:

sample 代码:

if os.path.exists(dst):
  shutil.rmtree(dst, ignore_errors=False)
  shutil.copytree(src, dst,ignore=shutil.ignore_patterns(".git"))

自己写 loop

比如用 os.walk 或者 glob.glob

不详述了。建议是,能不造轮子就不造。但,如果是初学者,自己写则非常推荐。

上一篇 下一篇

猜你喜欢

热点阅读