我是 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/