title: Python assign function to dict value
body: <p>I am trying to write a script that opens files based on dictionary values. For each key/value it opens a file based on that value's name and assigns that file to the name of the value (I think it's going wrong here). So far it opens the files for me, with the right names, so that works. However, I think the name I assign the open(file) function to is wrong, since the rest of my function does not open the files anymore, and I can't close them.
  Example of my script:</p>
  
  <pre><code>filelist=[]
  codeconv={"agt":"r1p1d", "aga":"r2p1d"}
  for value in codeconv.values():
      value=open("c:\Biochemistry\Pythonscripts\Splittest\split"+value+".txt", 'w')
      filelist.append(value)
  
  "Here I do something with the files"
  
  for f in filelist:
      f.close()
  </code></pre>
  
  <p>So the problem is, how do I assign the open(file) function to the correct name? (Here r1p1d and r2p1d for example) And how do I later call those again?</p>
  
  <p>The error I get now is:
  AttributeError: 'str'object has no attribute 'close' 
  on the f.close() line.</p>
  
  <p>EDIT: It now works as I want it to, using the following code: (I also included the 'here I do something' part now just for clarity)</p>
  
  <pre><code>result=open("C:\\Biochemistry\\Pythonscripts\\Illuminaresults.txt", "r")
  filelist=[]
  
  codeconv={"agt":"r1p1d", "aga":"r2p1d"}        
  opened_files={}
  for key, value in codeconv.items():
      filename="c:\Biochemistry\Pythonscripts\Splittest\split"+value+".txt"
      file=open(filename, 'w')
      opened_files.update({key: file})
  
  for line in result:
      if line[0]=="&gt;":
          lastline=line
      if line[0:3] in codeconv and len(line)==64:
          f=opened_files[line[0:3]]
          f.write(lastline+line)
      else: continue 
  
  for f in opened_files.values():
      f.close()
  
  result.close()
  </code></pre>
  
  <p>I got another problem now though when I try to write the next part of my script, but that's probably something for another question, as it gives a Windowserror not related to this part. Thanks for the help all!</p>