How to execute one python file with the arguments from another python file without passing arguments on command/terminal line?
Question: I have a default code in Python that has the argument parser and have to pass
those argument by myself in command line,but i dont want to pass the arguments
myself rather execute that file from another python file with the arguments
written in that file or i dont want to write those arguments myself on command
lines.

My argument parsing code is as follows:

    
    
    if __name__ == "__main__":
         parser = argparse.ArgumentParser()
         parser.add_argument('-i', type=str, nargs='+', help="Enter the filenames with extention of an Image")
         arg=parser.parse_args()
    
        if not len(sys.argv) > 1:
        parser.print_help()
        exit()
    

my function is

    
    
    def Predict_Emotion(filename):
          print "Opening image...."
          try:
              img=io.imread(filename)
              cvimg=cv2.imread(filename)
         except:
               print "Exception: File Not found."
               return
    

And my execution line is as

    
    
    for filename in arg.i:
         Predict_Emotion(filename)
    

Any help would be appreciated.


Answer: Here is how you do it. You need to slightly modify the above code to form the
following

    
    
    import sys, argparse
    
    def run_function (lista):
        parser = argparse.ArgumentParser()
        parser.add_argument('-i', type=str, nargs='+', help="Enter the filenames with extention of an Image")
        arg=parser.parse_args(lista)
        if (len(sys.argv) <= 1):
            parser.print_help()
            exit()
    
    if __name__ == "__main__":
        run_function (sys.argv)
    

Reference used: <https://docs.python.org/2/library/argparse.html>

That will now allow you to give it your own list from another file. To call it
from the other file, you need to do the following

    
    
    #These below two lines are not necessary if stating python in directory the script is in, or if it is already in your Python Path
    import sys
    sys.path.insert(0, "path to directory script is in")
    #Below lines are now necessary
    import your_script #NOTE this is the name of your file without the .py extention on the end
    list_of_files = ["file1", "file2"]
    your_script.run_function(list_of_files)
    

I think that is everything you asked!

