Linking Dictionary Values

Corrado Gioannini gioco at
Fri Aug 24 17:12:45 CEST 2001

On Fri, Aug 24, 2001 at 09:31:44AM -0500, Maan M. Hamze wrote:
> Is it possible to link Dictionary values of each key into a text file?  So
> the value of a key is a text file somewhere on the PC.  And if so, how do I
> instruct Python to open the text file and print into screen when the key of
> the text file is used?
you can do it by just putting the file name in the dictionary and define a
function to print its content:

dict = {'key1':'/path/file1', 'key2':'/path/file2'}

def printFile(filename):
    f = open(filename)

and then just call:


> Also, is it possible to link a value of a Dictionary key into a Python
> function/procedure?  So, if the key is encountered, a way for the
> functionprocedure to be run.

yes, just do it:

def exampleFunction():
    print 'something'

dict = {'key': exampleFunction}

then you can for example:




Corrado Gioannini
<gioco at>

"Thought is only a flash between two long nights,
                                         but this flash is everything."
                                                          (H. Poincaré)

More information about the Python-list mailing list