You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用shutil.copyfile复制文件时遇权限/文件缺失错误的解决

文件复制权限与路径问题排查及解决

第一次尝试的代码及错误

参照网上示例编写的文件复制代码:

import os
import shutil
from pathlib import Path

DATA_DIR = Path.cwd() / 'sourceFolder'
files = os.listdir(DATA_DIR)
shutil.copyfile(os.path.join('sourceFolder', files[0]), '/destFolder')

即便拥有目标目录和文件的全部必要权限,仍触发错误:

PermissionError: [Errno 13] Permission denied: '/destFolder'

第二次尝试的代码及错误

换用另一示例代码后:

DATA_DIR = Path.cwd() / 'sourceFolder'
files = os.listdir(DATA_DIR)
des = os.path.join('/destFolder', files[0])
shutil.copyfile(os.path.join('sourceFolder', files[0]), des)

收到新的错误:

FileNotFoundError: [Errno 2] No such file or directory: '/destFolder/nameOfFile'

最终解决代码

通过以下代码成功解决问题:

dst = Path.cwd() / 'destFolder/nameOfFile'
shutil.copyfile(os.path.join('sourceFolder', files[0]), dst)

感谢J的帮助。

内容的提问来源于stack exchange,提问作者JF0001

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 07:35:11