Python os.makedirs 和 shutil.copyfile - 错误 13 - 权限被拒绝

Python os.makedirs and shutil.copyfile - Error 13 - Permission denied

在我的代码中,我创建了一个目录,如下所示:

try:
    os.makedirs(playlist_name)
except OSError as e:
    if e.errno != errno.EEXIST:
        raise

它在我 运行 我的 python 脚本所在的地方创建了一个目录。 然后我想从文件夹所在的原始目录中复制三个文件到新建的目录中,像这样

# Copy FFMPEG files into that folder so youtube dl can download the videos as audio tracks
# Tried using os.getcwd() to get full path, same error
shutil.copyfile(os.getcwd() + '\ffmpeg.exe', os.getcwd() + "\" + playlist_name)
shutil.copyfile('ffplay.exe', "/" + playlist_name + "/")
shutil.copyfile('ffprobe.exe', "/" + playlist_name + "/")

但是,尝试复制这些文件会引发此错误:

PermissionError: [Errno 13] Permission denied: 'C:\Users\ME\Documents\python\DIRECTORY\PLAYLIST_NAME_HERE'

我试过各种shutil复制方法都出现同样的错误。

编辑:这是 运行宁 windows

根据 the copyfile docs:

dst must be the complete target file name; look at shutil.copy() for a copy that accepts a target directory path.

您不能使用它来执行您在 shell 中执行的操作,命名一个源文件和一个目标目录并让它推断出该文件应该放在具有文件原始名称的目录中。你必须明确命名目标文件,否则它认为你试图复制到与目录相同的名称,并且与替换文件不同,你不能在不明确重命名目录或删除目录的情况下用文件替换目录首先是整个目录树。要修复,只需确保在源和目标中重复文件名:

for filename in ('ffmpeg.exe', 'ffplay.exe', 'ffprobe.exe'):
    shutil.copyfile(filename, os.path.join(playlist_name, filename))

这个问题在类似 UNIX 的系统上会更加明显,因为这些系统会拒绝 EISDIR 的操作,导致 Python IsADirectoryError 被引发,但是 Windows 出于某种原因选择使用与 permission/access 问题相关的更一般的错误代码(EACCES 和相关的 Windows 特定错误代码),Python 转换为 PermissionError(因为 Windows 只是没有告诉它真正的问题,如果 Python 试图检查真正的问题是否是试图将目录用作文件,它会引入各种竞争条件修复异常类型)。