尽管代码工作正常,Python 仍抛出错误

标签 python shutil

我有一些Python代码如下:

folderPath = 'C:\Program Files (x86)\Program\folder/' + folderName
if not os.path.exists(folderPath):
    shutil.copytree('C:\Program Files (x86)\Program\folder\anotherfolder', folderPath)

变量folderName来自用户输入,本质上我的程序允许用户创建一个文件夹,然后将另一个文件夹中的一些内容复制到新文件夹中。您可以将其视为某种文件备份系统。

最奇怪的是这个。它工作完美,创建了文件夹(如 Shutil 文档中所述)并复制了其他文件夹的内容,但是,出现了错误:

[error] script [ myScript ] stopped with error in line 52
[error] shutil.Error ( ['C:\\Program Files (x86)\\Program\\folder\\anotherfolder', 'C:\\Program 
Files (x86)\\Program\\folder\\test', "[Errno 5] Input/output error: 'C:\\\\Program Files 
(x86)\\\\Program\\\\folder\\\\test'"] )

在本例中,我输入的文件夹名称是“test”。第 52 行是 Shutil.copytree() 调用。

即使文件全部被复制并且运行良好,我的脚本也会停止运行。

如何忽略此问题(如果可能)并继续执行脚本?或者如果这确实是我的代码的问题,我该如何解决这个问题?

感谢所有帮助。

提前致谢。

最佳答案

进一步挖掘:

根据microsoft ,errno 5对应访问被拒绝。

copytree 使用 copy2() 复制文件,然后更改其权限、次数。

恕我直言,您无权更改(或检索)文件属性,因此您将获得具有您自己拥有的任何属性(您的用户、组...)的文件。

HTH

关于尽管代码工作正常,Python 仍抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21425811/

相关文章:

python - 通过两个函数运行循环,将彼此的输出作为输入

Python Telegram Bot : Prompt for another input

python - 根目录和基本目录有什么区别?

python - 从 Python 中的函数返回函数

python - 如何使用python脚本同步两个文件夹

python argparse.ArgumentParser 读取配置文件

Python shutil copyfile - 缺少最后几行

python - ARINC429 构词法

python - 无法构建使用 Python 的项目

python - 如何获取进程的祖 parent ID