Why am I getting no attribute '__getitem__' error for dictionary?
Asked Answered
S

3

16

Why am I getting no attribute __getitem__ error for dictionary:

Traceback (most recent call last):
  File "./thumbnail.py", line 39, in <module>
    main()
  File "./thumbnail.py", line 19, in main
    options['input_pattern']
AttributeError: Values instance has no attribute '__getitem__'

Here's the code:

#!/usr/bin/env python

import os, sys, glob
from PIL import Image
from optparse import OptionParser

def batch_convert(src_dir, input_pattern, output_ext = None, dest_dir = None):
    return 0

def main():
    print sys.argv
    parser = OptionParser()
    parser.add_option("-s", "--source-dir", dest="src_dir", help="Source directory to fetch images")
    parser.add_option("-d", "--dest-dir", dest="dest_dir", help="Destination directory to writen processed images")
    parser.add_option("-i", "--input-pattern", dest="input_pattern", help="Look for files that match some pattern. E.g. *.png or pic*cool*")
    parser.add_option("-o", "--output-format", dest="output_ext", help="Output format to save all images. If empty, original format of images is preserved")
    (options, args) = parser.parse_args()
    print options
    options['input_pattern']

if __name__ == "__main__":
    main()
Sandal answered 25/2, 2010 at 1:43 Comment(0)
P
25

options is not a dict:

print options.input_pattern
Precocity answered 25/2, 2010 at 1:47 Comment(0)
A
7

If you must have a dict, use

options.__dict__
Afresh answered 25/6, 2010 at 18:5 Comment(0)
C
0

As another user said, options is not a dictionary. I made this mistake myself. If I have an option --file FILE

Then I can call

option.file

which returns FILE. It's that easy!

Cylindroid answered 3/8, 2015 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.