<br>I can't see the forest through the trees.<br><br>I have stored 3 global variables in a dictionary, and associated each variable with a filename.<br>Ideally, I want to read the contents of the text files, and store the contents in the global variables. The globals will be used by another function.
<br>However, when I do the assignment to varname = fh.readlines(), a new variable is created, and the reference to the global variable is overwritten, because the contents of the files are strings, and strings are immutable.
<br><br>I see the problem, but not a good solution.<br><br><br>var1=""<br>var2=""<br>var3=""<br><br>def initGlobals():<br> <br> global var1, var2, var3<br><br> fileDict = {'var1.txt
':var1, 'var2.txt':var2, 'var3.txt':var3}<br><br> for fn, varname in fileDict.iteritems():<br> try:<br> try:<br> fh=open(fn, 'r')<br> #id(varname) # at this point, this id matches the id of the global variable
<br> varname = fh.readlines() # this creates a new variable, but I want to store the file contents in the global var<br> #id(varname) # this is a new id, the global var is not updated
<br> fh.close()<br> except IOError:<br> print "\nFATAL ERROR occurred reading %s\n" % fn<br> finally:<br> fh.close()<br> <br><br>