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
  How to not open the context menu if a mouse gesture is executed? MicheliBarcello 2 684 Aug-22-2023, 02:47 PM
Last Post: deanhystad
  Context-sensitive delimiter ZZTurn 9 1,575 May-16-2023, 07:31 AM
Last Post: Gribouillis
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 824 May-02-2023, 08:40 AM
Last Post: Gribouillis
  How does open context manager work? deanhystad 7 1,384 Nov-08-2022, 02:45 PM
Last Post: deanhystad
  Decimal context stevendaprano 1 1,070 Apr-11-2022, 09:44 PM
Last Post: deanhystad
  How to Properly Open a Serial Port in a Function bill_z 2 4,549 Jul-22-2021, 12:54 PM
Last Post: bill_z
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,317 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,520 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  Execution of Another Recursive Function muzikman 5 3,054 Dec-04-2020, 08:13 PM
Last Post: snippsat
  Don't Understand Recursive Function muzikman 9 3,763 Dec-03-2020, 05:10 PM
Last Post: muzikman

Forum Jump:

User Panel Messages

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