当前位置: 代码网 > it编程>前端脚本>Python > Python实现文件/文件夹复制功能

Python实现文件/文件夹复制功能

2025年04月01日 Python 我要评论
一、基础工具库介绍python标准库中的shutil模块是主要工具:import shutilimport os二、文件复制基础1. 复制单个文件# 保留元数据(修改时间等)shutil.copy2(

一、基础工具库介绍

python标准库中的shutil模块是主要工具:

import shutil
import os

二、文件复制基础

1. 复制单个文件

# 保留元数据(修改时间等)
shutil.copy2('source.txt', 'destination.txt')

# 仅复制内容
shutil.copy('source.txt', 'backup/')  # 自动保留文件名

# 文件流方式(适合大文件)
with open('source.txt', 'rb') as src, open('dest.txt', 'wb') as dst:
    shutil.copyfileobj(src, dst, length=16*1024)  # 16kb缓冲区

三、目录复制进阶

1. 简单目录复制

# 复制整个目录(目标目录必须不存在)
shutil.copytree('src_dir', 'dst_dir')

# 允许覆盖已存在目录(python 3.8+)
shutil.copytree('src', 'existing_dir', dirs_exist_ok=true)

2. 自定义复制过程

def ignore_patterns(*patterns):
    def _ignore(path, names):
        ignored = []
        for pattern in patterns:
            ignored.extend(fnmatch.filter(names, pattern))
        return set(ignored)
    return _ignore

# 排除.pyc文件和临时文件
shutil.copytree('src', 'dst', ignore=ignore_patterns('*.pyc', '*.tmp'))

# 带进度回调的复制
def copy_progress(src, dst, *, follow_symlinks=true):
    print(f"copying {src} => {dst}")

shutil.copytree('src', 'dst', copy_function=copy_progress)

四、高级自定义实现

当需要完全控制复制流程时,可以手动实现:

def deep_copy(src, dst, symlinks=false):
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    
    for item in os.listdir(src):
        src_path = os.path.join(src, item)
        dst_path = os.path.join(dst, item)
        
        if os.path.isdir(src_path):
            deep_copy(src_path, dst_path, symlinks)
        else:
            if os.path.exists(dst_path):
                if os.path.samefile(src_path, dst_path):
                    continue
                os.remove(dst_path)
            if symlinks and os.path.islink(src_path):
                linkto = os.readlink(src_path)
                os.symlink(linkto, dst_path)
            else:
                shutil.copy2(src_path, dst_path)

五、异常处理要点

try:
    shutil.copytree('src', 'dst')
except shutil.error as e:
    print(f'directory not copied. error: {e}')
except oserror as e:
    print(f'oserror: {e.strerror}')

六、性能优化建议

  • 批量小文件:使用shutil.copytree的默认实现
  • 超大文件:使用copyfileobj分块复制
  • 网络存储:增加缓冲区大小(例如16*1024*1024即16mb)
  • 并行处理:对独立子目录使用多线程/多进程

七、完整示例代码

import shutil
import os
from pathlib import path

def smart_copy(src, dst, overwrite=false, ignore=none):
    """智能复制器"""
    src = path(src)
    dst = path(dst)

    if src.is_file():
        if dst.is_dir():
            dst = dst / src.name
        if dst.exists():
            if overwrite:
                dst.unlink()
            else:
                raise fileexistserror(f"{dst} already exists")
        shutil.copy2(str(src), str(dst))
        return

    if not src.is_dir():
        raise valueerror("source path invalid")

    if dst.exists():
        if overwrite:
            if dst.is_dir():
                shutil.rmtree(dst)
            else:
                dst.unlink()
        else:
            raise fileexistserror(f"{dst} already exists")

    shutil.copytree(
        str(src),
        str(dst),
        symlinks=true,
        ignore=ignore,
        copy_function=shutil.copy2,
        dirs_exist_ok=overwrite
    )

# 使用示例
smart_copy(
    '/data/project',
    '/backup/project_2024',
    overwrite=true,
    ignore=shutil.ignore_patterns('*.log', 'temp')
)

关键点说明:

  • 使用copy2而不是copy可以保留文件元数据
  • path对象比字符串路径更安全易用
  • dirs_exist_ok参数需要python 3.8+
  • 自定义ignore模式支持复杂过滤逻辑
  • 完善的异常处理保证操作可靠性

这种方法可以处理以下复杂场景:

  • 混合文件类型(普通文件/符号链接/特殊文件)
  • 保留所有文件属性
  • 覆盖已有内容的安全处理
  • 灵活的过滤机制
  • 跨平台兼容性(windows/unix)

以上就是python实现文件/文件夹复制功能的详细内容,更多关于python文件/文件夹复制的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com