How do I copy a folder and its contents (files/subdirectories) in Python with platform independent implementation

筅森魡賤 提交于 2021-02-07 12:14:29

问题


I need a function in python that lets me specify the source and destination paths for a folder and copies the source folder recursively into the destination folder. The implementation I am looking for needs to be platform independent


回答1:


You could use shutil.copytree:

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; it will be created as well as missing parent directories. Permissions and times of directories are copied with copystat(), individual files are copied using shutil.copy2().


import shutil
shutil.copytree(src, dst)


来源:https://stackoverflow.com/questions/27592258/how-do-i-copy-a-folder-and-its-contents-files-subdirectories-in-python-with-pl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!