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.
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.
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.
I appreciate any help offered.
def MakeRequest(i):
# some code to call api removed
xml_string = "<list><FilterItems><FilterItem attribute='pageNumber' value='%d' /></FilterItems><SortItems><SortItem attribute ='activity_aud_mem_id' sortOrder='0'/></SortItems></list>" % 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);
I get the following error at the f = open line.
TypeError: "unsupported operand type(s) for %: 'file' and 'int'"
Use this:
The string formatter operator
%works on the preceding string, so you have to put it directly behind it.I would recommend to reformat the whole block like this:
Then you need no
f.close()