2

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

0

3 Answers 3

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:

Sign up to request clarification or add additional context in comments.

Comments

1

import - will execute code which you import (once)

os.system("scriptname.py")

subprocess

popen

3 Comments

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?
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.
Ok, then we agree. Cheers.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.