python - Errno 22 无效 (wb) 或文件名。 Python Shutil.copy

标签 python copy shutil

我是 python 初学者,我对这个脚本有疑问:

import errno import shutil import os  
        def copystuff(src, dest):
        if os.path.isfile(src):
            dest_dirname = os.path.dirname(src)
            if not os.path.isdir(dest_dirname):
                os.makedirs(dest_dirname)
            shutil.copy2(src, dest)
        else:
            shutil.copytree(src, dest)       
    copystuff('C:\\Downloads\\index.html', 'J:\\include\\')

其中 J 是 FlashDriveUSB,我使用的是 Python 2.7

当我启动这个时,我得到了类似的东西:

C:\Python27>python copy_file.py
Traceback (most recent call last):
  File "copy_file.py", line 24, in <module>
    copystuff('C:\\Downloads\index.html', 'D:\\include\\')
  File "copy_file.py", line 20, in copystuff
    shutil.copy2(src, dest)
  File "C:\Python27\lib\shutil.py", line 127, in copy2
    copyfile(src, dst)
  File "C:\Python27\lib\shutil.py", line 82, in copyfile
    with open(dst, 'wb') as fdst:
IOError: [Errno 22] invalid mode ('wb') or filename: 'D:\\include\\'

请帮助我。

最佳答案

我解决了我的问题。我想将文件复制到不存在的文件夹,因此我在代码中添加了几行代码,检查输入的路径是否存在,如果不存在则创建路径。

关于python - Errno 22 无效 (wb) 或文件名。 Python Shutil.copy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29257034/

相关文章:

python - 处理后移动文件

python - 使用 .loc 设置WithCopyWarning

python - 复制文件,但不要覆盖,Python 中没有 TOCTTOU 问题

python - celery 没有同时执行任务

python - 在 py2neo 中添加标签不起作用

python - 在 PyQt 中删除 QTreeWidgetItem?

python - 运行 Spark 时遇到 "WARN ProcfsMetricsGetter: Exception when trying to compute pagesize"错误

vba - Excel从文件复制到文件宏不起作用

C# 跟踪复制的文件

python-3.x - python : move a file up one directory