Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

how to execute a python script file with an argument from inside another python script file

my problem is that I want to execute a python file with an argument from inside another python file to get the returned values....

I don't know if I've explained it well...

example:

from the shell I execute this:

          getCameras.py "path_to_the_scene"

and this return me a list of cameras....

so how can I call this script (including the argument) from another script ???

I've been trying to figure it out by myself by reading some other questions here , but I didn't get it well, should I use the execfile() function?? how exactly??

Thanks in advance for helping a newbie like me!!

Ok, after take a look at your answers, I have to edit my question to make it more concise and because I don't understand some answers(sorry, like I said I'm a newbie!!!):

Well, I have this 2 scripts "getMayaCameras.py" and "doRender.py" and one more called "renderUI.py" that implements the first 2 scripts in a GUI.

"getMayaCameras.py" and "doRender.py" are both scipts that you can execute directly from the system shell by adding an argument ( or flags, in the "doRender.py" case) and, If it is possible, I want to still having this posibility so I can choose between execute the UI or execute the script dirctly from the shell

I've made already some modifications for them to work by importing them from the "renderUI.py" script but now they don't work by themselves....

So is possible to have this scripts working by themselves and still having the possiblity of calling them from another script? how exactly? This "separating the logic from the command line argument handling" that you told me before sounds good to me but I don't know how to implement it on my script ( I tried but without succes) ....

That's why I'm posting here the original code for you to see how I made it, feel free both to make critics and/or correct the code to explain me how should I make it for the script to work properly...

#!/usr/bin/env python

import re,sys

if len(sys.argv) != 2:
    print 'usage : getMayaCameras.py <path_to_originFile> 
You must specify the path to the origin file as the first arg'
    sys.exit(1)


def getMayaCameras(filename = sys.argv[1]): 
    try:
        openedFile = open(filename, 'r')
    except Exception:
        print "This file doesn't exist or can't be read from"
        import sys
        sys.exit(1)

    cameras = []    
    for line in openedFile: 
        cameraPattern = re.compile("createNode camera")     
        cameraTest = cameraPattern.search(line) 
        if cameraTest:      
            cameraNamePattern = re.compile("-p[s]+"(.+)"")           
            cameraNameTest = cameraNamePattern.search(line)         
            name = cameraNameTest.group(1)          
            cameras.append(name)            
    openedFile.close()

    return cameras      

getMayaCameras()

Thanks again,

David

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The best answer is don't. Write your getCameras.py as

import stuff1
import stuff2 
import sys

def main(arg1, arg2):
    # do whatever and return 0 for success and an 
    # integer x, 1 <= x <= 256 for failure

if __name__=='__main__':
    sys.exit(main(sys.argv[1], sys.argv[2]))

From your other script, you can then do

import getCamera

getCamera.main(arg1, arg2)

or call any other functions in getCamera.py


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...