-1

I've got a couple of scripts (named one.py and two.py) circularly calling each other using execfile. The one.py (which is the one to start) has some code (initialize) that I would like to execute only once.

I would like to continue using execfile if possible

How could I achieve this ?

#one.py
def initialize():         
    # initializing

initialize()

# do the main work

execfile('two.py')

----------------------------

#two.py

# do some work

execfile('one.py')
user1530405
  • 447
  • 1
  • 8
  • 16

2 Answers2

2

Why not create a third file zero.py which starts the initializing and then executes one.py who then executes the loop.

 #zero.py
 def initialize():
# do some initializing

initialize()

execfile('one.py')

On another note there should probably be better ways to run your code then this loop of execfile.

PdevG
  • 3,427
  • 15
  • 30
0

Maybe you could define a class that runs your tasks?

For instance, something like:

from itertools import cycle

class TaskRunner(object):

    def __init__(self):
        self.tasks = ('one.py', 'two.py')

        # do some initializing

    def run_continuously(self):
        for filepath in cycle(self.tasks):
            execfile(filepath)


# to run:
task_runner = TaskRunner()
tsak_runner.run()

----------------------------

#one.py

# do the main work (without initializing)


----------------------------

#two.py

# do some work

----------------------------
Kris
  • 22,079
  • 3
  • 30
  • 35