Skip to main content
 首页 » 编程设计

python之无效的文件名或模式 'wb'

2024年11月01日11qq78292959

我正在使用 Django。我试图使用 Pythons 图像库将用户上传的图标压缩到较小的尺寸。

以下是我的代码:

def resizeImage(icon,ext): 
     path= os.path.join(settings.SITE_ROOT,'karnadash/static/tempfiles/temp'+ext) 
     destination = open(path,'wb+') 
     for chunk in icon.chunks(): 
         destination.write(chunk) 
     destination.close() 
     image = Image.open(path) 
     image= image.resize((50, 50), Image.ANTIALIAS) 
     image.save(path) 
     return image 

问题是我收到内部服务器错误。堆栈跟踪的最后一部分如下:

 line 31, in resizeImage 
     image.save(path) 
 File "C:\Python27\lib\site-packages\PIL\Image.py", line 1446, in save 
     fp = builtins.open(fp, "wb+") 
IOError: [Errno 22] invalid mode ('wb') or filename: 'C:/Users/Silent/Documents/Python/karnadash/karnadash/static/tempfiles/temp.jpg' 

谁能解释一下为什么会这样?

请您参考如下方法:

为我解决的是从反斜杠切换到正斜杠!谁会想到?!

类似的帖子:ioerror invalid mode w