How to fix those errors?

Thomas 'PointedEars' Lahn PointedEars at web.de
Sun Nov 16 23:04:51 CET 2014


Abdul Abdul wrote:

> from PIL import Image
> import os

You should only import the methods that you use.
 
>     for inputfile in filelist
>     outputfile = os.path.splitext(inputfile)[0]+".jpg"
>     if inputfile != outputfile:
>         try:
>             Image.open(inputfile).save(outputfile)
>             except IOError:
>             print "unable to convert ", inputfile
              ^^^^^^
If possible, write new programs in Python 3.x (where paretheses are required 
for print()).
 
> I'm writing that in "Pycharm",

Try PyDev or LiClipse with PyDev instead.

> and getting the following errors:
> 
> * filelist ---> Unresolved reference 'filelist'

Define “filelist” which needs to refer to an iterable value.  Also, you have 
forgotten to end the “for…in” statement with a semicolon, and you need to 
unindent it one level if it is to make sense.
 
> * IOError: Statement seems to have no effect

You have indented the “except” clause one level too many.  It must start at 
the same column as the corresponding “try” clause.  In Python, indentation 
works like block braces in C-like programming languages.  Do not mix spaces 
and tabs for indentation.
 
> * The last 'filelist' ---> 'except' or 'finally' expected

Follows from the above.  Standalone “try” clauses are not allowed.
 
> How can I solve those errors?

See above.  Summary (untested):

from PIL import Image
from os.path import splitext

filelist = ["foo", "bar"]

for inputfile in filelist:
    outputfile = splitext(inputfile)[0] + ".jpg"
    if inputfile != outputfile:
        try:
            Image.open(inputfile).save(outputfile)
        except IOError:
            print("unable to convert", inputfile)


Please do not post multi-part messages.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.



More information about the Python-list mailing list