2

I need another python script that will execute these 3 scripts.

SpringField
  • 127
  • 1
  • 2
  • 14

3 Answers3

3

You probably want the following :

import os

def include(filename):
if os.path.exists(filename): 
    execfile(filename)


include('myfile.py')

But I think it would be better to refactor your code using functions and use import . There already was similar questio at SO:

Community
  • 1
  • 1
ENargit
  • 614
  • 1
  • 5
  • 9
1

import - will execute code which you import (once)

os.system("scriptname.py")

subprocess

popen

forvaidya
  • 3,041
  • 3
  • 26
  • 33
  • Once? If he/she has written the code well, the import will let him/her use functions, for example, so I don't think the "once" is correct, is it? – Jblasco Aug 01 '13 at 08:09
  • Sir,I interpreted execute (in op) as in execution of 'program' rather than a function call. If it is function/method call then it is more than once. – forvaidya Aug 01 '13 at 08:16
  • Ok, then we agree. Cheers. – Jblasco Aug 01 '13 at 08:21
1

Put the python code to be called into a namespace/module that is visible to python through sys.path and import the methods/classes in your secondary .py files. That way you can directly access the code and execute it the way you require.

Like other answers already suggest you could execute the code in your secondary files directly but I would personally always prefer to package it up and import it - it's much cleaner and easier to maintain as you can do more selective changes to the code in your secondary files without affecting any part that imports already existing parts of it.

bossi
  • 1,629
  • 13
  • 17