简体   繁体   中英

Calling Python file with command line arguments from another python file

Currently my python code is called from command line as follows:

C:\project-master>python myFile.py --plugin TestPlugin\migration -f "C:/Form1/All_2020-01-01.xsd"

Where myFile.py code needs commandline arguments:

--plugin TestPlugin\migration
-f "C:/Form1/All_2020-01-01.xsd"

I have created a new python file called: Test.py . From Test.py file, how can I make a call to myFile.py along with the required arguments. For example-

class Workflow:

    def Process(self):
        # How to make a call for below commandline from here: 
        # myFile.py --plugin TestPlugin\migration -f "C:/Form1/All_2020-01-01.xsd"

A = Workflow()
A.Process()

Python subprocess module is your friend on this one.

You can use the run() function of the subprocess module. Your execution command is given as a list of strings as the first arugment in run()

So in your case it should look like this:

import subprocess

subprocess.run(["python", "myFile.py", "--plugin", "TestPlugin\migration", "-f", "C:/Form1/All_2020-01-01.xsd"], shell=False)

You can put that line into your Process() method.

Also have a look in the documentation if you want to catch the output eg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM