0

I am using Windows 10 and Python 2.7.12, I have observed that when my folder starts with a letter f such as "folder1", "fig", "fish" it gives me an error:

[Errno 22] invalid mode ('wb') or filename:

When I rename my folder to any word starting with any letter that is not f, then it succeeds. Any reason why this happens? This is the code that I was running.

import os
import os.path
#From PIL
import Image

path2 = 'C:\Users\f'
def tiff_to_jpeg(path):
    for root, dirs, files in os.walk(path):
        for f in files:
            a = f[:-4].replace(" ","")
            b = a.replace("(","")
            c = b.replace(")","")
            c = c[2:]
            c = '01' + c
            print c
            if os.path.splitext(os.path.join(root,f))[1].lower() == ".tif":
                # If a jpeg is already present. Don't do anything.
                if os.path.isfile(os.path.splitext(os.path.join(root, f))[0] + ".jpg"):
                    print "A jpeg file already exists for %s" %f
                    # If a jpeg is *NOT* present, create one from the tiff.
                else:
                    outfile = os.path.join(path2,c) + ".jpg"
                    #outfile = os.path.splitext(os.path.join(root,f))[0] + ".jpg"
                    try:
                        im = Image.open(os.path.join(root,f))
                        print "Generating jpeg for %s" %f
                        im.thumbnail(im.size)
                        im.save(outfile, "JPEG", quality=100)
                    except Exception, e:
                        print e

path = 'C:\Users\f'
tiff_to_jpeg(path)
Gerard
  • 518
  • 4
  • 19
  • the slash character "\" is used as an escape by Python. To avoid that, either escape it (replace all single slashes on your file path with double ones "\" => "\\") or add the letter "r" in front of the provided file paths like: `path = r'C:\Users\f'`. "\f", if not escaped, is interpreted as a form feed (see: http://stackoverflow.com/questions/4334370/escape-sequence-f-form-feed-what-exactly-is-it) – Ma0 Aug 09 '16 at 08:19
  • Thanks for the explanation. These are those little things I should be more aware of. Thanks again! – Gerard Aug 09 '16 at 08:27

0 Answers0