Copy file or directories recursively in Python

前端 未结 5 883
无人及你
无人及你 2020-11-27 12:17

Python seems to have functions for copying files (e.g. shutil.copy) and functions for copying directories (e.g. shutil.copytree) but I haven\'t fou

5条回答
  •  有刺的猬
    2020-11-27 12:45

    I suggest you first call shutil.copytree, and if an exception is thrown, then retry with shutil.copy.

    import shutil, errno
    
    def copyanything(src, dst):
        try:
            shutil.copytree(src, dst)
        except OSError as exc: # python >2.5
            if exc.errno == errno.ENOTDIR:
                shutil.copy(src, dst)
            else: raise
    

提交回复
热议问题