Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6686007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:07:23+00:00 2026-05-26T05:07:23+00:00

I am calling a function and need to loop through a range calling the

  • 0

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'"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:07:24+00:00Added an answer on May 26, 2026 at 5:07 am

    Use this:

    f = open('/Users/output_test_%d.txt' % i, 'w')
    

    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:

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

    Then you need no f.close()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I need to write a function which returns a value for a input
I need to write a function in JavaScript, which returns a state from calling
I need create a tree structure recursively. In the tree each node has different
I'm calling a function with call_user_func_array : call_user_func_array(array($this, 'myFunction'), array('param1', 'param2', 'param3')); Everything is
I'm new to PHP and I'm confused seeing some examples calling a function with
I am (successfully) calling the Windows FilterSendMessage function in c# using the following pinvoke
Calling Validate() on an XmlDocument requires passing in a ValidationEventHandler delegate. That event function
Can it be assumed a evaluation order of the function parameters when calling it
Follow on from: Javascript wait for image to load before calling Ajax function initResources()
I have a javascript function that enters a loop and issues an asynchronous ajax

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.