简体   繁体   English

如何使用独立于平台的实现在Python中复制文件夹及其内容(文件/子目录)

[英]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. 我需要python中的一个函数,它允许我指定文件夹的源和目标路径,并将源文件夹递归地复制到目标文件夹中。 The implementation I am looking for needs to be platform independent 我正在寻找的实现需要独立于平台

You could use shutil.copytree : 你可以使用shutil.copytree

shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False) 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. 递归复制以src为根的整个目录树,返回目标目录。 The destination directory, named by dst, must not already exist; 目标目录(由dst命名)必须不存在; 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(). 使用copystat()复制目录的权限和时间,使用shutil.copy2()复制单个文件。


import shutil
shutil.copytree(src, dst)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在多个子目录中使用python向文件添加扩展名 - How do I add extensions to files using python in multiple subdirectories 如何在Python中压缩文件夹及其所有文件,同时保留其内容的文件夹名称和相对路径? - How to zip a folder and all of its files in Python, while preserving the folder name and relative paths for its contents? Python pathlib.Path - 我如何获得一个独立于平台的文件分隔符作为字符串? - Python pathlib.Path - how do I get just a platform independent file separator as a string? 如何将bytearray的内容复制到列表(Python)? - How do I copy the contents of a bytearray to a list (Python)? Python:如何通过比较列表的内容将列表拆分为多个? - Python: How do I split a list into multiple by comparing its contents? 如何使用python读取Windows历史记录(文件夹)的内容? - How do I read the contents of Windows History (folder) using python? Python 3 - 如果目标文件夹中不存在文件,则复制文件 - Python 3 - Copy files if they do not exist in destination folder Python计算目录及其所有子目录中的文件 - Python count files in a directory and all its subdirectories 如何只复制python3文件夹中的文件? - How to copy just the files in a folder in python3? 如何使用递归列出目录和子目录中的现有文件? - how do i list existing files in a directory and subdirectories using recursion?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM