Python Forum
with open context inside of a recursive function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
with open context inside of a recursive function
#1
I'm writing code to check various routers, part of it includes checking the traffic statistics, which is parsed per router. So I need either an open('file.txt')

or a with open context. I decided to stick with the context assuming it would be open until the entire program executed. The brlow for loop gets called by an outer function which is reccursed. Is this with context okay usage?


 for intf in interface_list:
            show_intf = "show interface {}".format(intf)
            result = ConnectHandler.send_command(show_intf)
            rate = re.search(input_rate, result)
            bandwidth = re.search(bw_template, result)
            if int(rate.group("rate")) / int(bandwidth.group("bandwidth")) >= 0:
                with open()
                updated_interfaces.append(intf)
or should I stick with traditional open, close
Reply
#2
Is the idea that you want to write values to file.txt and you want the file to stay open, but close when you are done?
def recursive_function(statistics, args):
    # Do router stuff
    # Write information to statistics

with open("file.txt", "w") as statistics:
    recursive_function(statistics, args)
Gribouillis likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Don't Understand Recursive Function muzikman 10 6,097 Mar-08-2025, 01:58 PM
Last Post: bterwijn
  Execution of Another Recursive Function muzikman 6 4,775 Mar-08-2025, 01:57 PM
Last Post: bterwijn
  not able to call the variable inside the if/elif function mareeswaran 3 585 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  Open CV cv2.bitwise_and() function rickyw2777 3 1,036 Feb-08-2025, 03:46 PM
Last Post: rickyw2777
  How to not open the context menu if a mouse gesture is executed? MicheliBarcello 2 1,426 Aug-22-2023, 02:47 PM
Last Post: deanhystad
  Context-sensitive delimiter ZZTurn 9 3,233 May-16-2023, 07:31 AM
Last Post: Gribouillis
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 1,918 May-02-2023, 08:40 AM
Last Post: Gribouillis
  How does open context manager work? deanhystad 7 2,774 Nov-08-2022, 02:45 PM
Last Post: deanhystad
  Decimal context stevendaprano 1 1,586 Apr-11-2022, 09:44 PM
Last Post: deanhystad
  How to Properly Open a Serial Port in a Function bill_z 2 8,275 Jul-22-2021, 12:54 PM
Last Post: bill_z

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020