Python variables in for loop returning TypeError
<p>I am calling a function and need to loop through a range calling the function for each value in the range. The max value is returned by a different function (findPageCount()) than the one I am having a problem with. </p>

<p>For MakeRequest() I can run it with the values hard-coded and it works just fine. When I add the for loop in and pass the integer in to the two lines of the function, I the the error listed at the bottom. I think this is a small formatting difference but I have not had success in tracking it down so far.  </p>

<p>The goal is to pass the i value in the for loop into the request in the xml string so the next page is shown and into the f = open statement so that a new file is created for that page. </p>

<p>I appreciate any help offered.</p>

<pre><code>def MakeRequest(i):
  # some code to call api removed
  xml_string = "&lt;list&gt;&lt;FilterItems&gt;&lt;FilterItem attribute='pageNumber' value='%d' /&gt;&lt;/FilterItems&gt;&lt;SortItems&gt;&lt;SortItem attribute ='activity_aud_mem_id' sortOrder='0'/&gt;&lt;/SortItems&gt;&lt;/list&gt;"   % i

  f = open('/Users/output_test_%d.txt', 'w') % i
  for line in r:
      try:
          f.write('%s\n' % line)
      except UnicodeEncodeError:
          pass

  f.close()

max_page = findPageCount()

for i in range(0, max_page): 
  MakeRequest(i);  
</code></pre>

<p>I get the following error at the f = open line.</p>

<pre><code>TypeError: "unsupported operand type(s) for %: 'file' and 'int'"
</code></pre>
