Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 434
Z3r0 C0oL Avatar asked Dec 21 '14 18:12

Z3r0 C0oL


1 Answers

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)
like image 181
unutbu Avatar answered Sep 20 '22 12:09

unutbu