python filename on windows -


disclaimer have similar thread started think got big , convoluted

in short problem

import imghdr import os.path .... image_type = imghdr.what(os.path.normpath(filename)) 

fails

ioerror: [errno 22] invalid mode ('rb') or filename: 'd:\\mysvn\\trunk\\assets\\models\\character\\char1.jpg\r' 

where aforementioned file exist

help? :d

there carriage return character \r @ end of filename. not valid character windows filename, doubt filename work.

use .rstrip('\r') remove it:

image_type = imghdr.what(os.path.normpath(filename.rstrip('\r'))) 

.rstrip() removes characters end of string, , in set name.

since filename, any whitespace around filename incorrect, straight-up .strip() work too:

image_type = imghdr.what(os.path.normpath(filename.strip())) 

this remove tabs, newlines, carriage returns , spaces both start and end of string.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -