Invalid file name or 'wb' mode

I am using django. I tried to compress an icon uploaded by a smaller user using the Pythons image library.

Below is my code:

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

The problem is that I get an internal server error. The last part of the stack trace is as follows:

 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'

Can someone explain why this is happening?

+2
source share
3 answers

For me it was a switch from backslash to slashes! Who would have thought?!

Related posts: ioerror invalid mode w

+2
source

Check your file path if valid:

C:/Users/Silent/Documents/Python/karnadash/karnadash/static/tempfiles/temp.jpg

Perhaps it contains one karnadashtoo much.

0

I had a similar problem when I was trying to save some dad numbers. I could save some numbers, but could not save others, and I used the same code. I realized that the shape name and backslash contradict the reserved code.

IOError: [Errno 22] invalid mode ('wb') or filename: '02102016\nDTG.png'

I think it was "\n"interpreted as "enter". The problem was resolved when I changed it to a slash.

-1
source

Source: https://habr.com/ru/post/1794563/


All Articles