目前,我的python代码是从命令行调用的,如下所示:

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


myFile.py代码需要命令行参数的地方:

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


我创建了一个新的python文件:Test.py。如何从Test.py文件以及必需的参数调用myFile.py。例如-

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子进程模块是您的朋友。

您可以使用run()模块的subprocess功能。您的执行命令以字符串列表形式给出,作为run()中的第一个整数

因此,在您的情况下,它应如下所示:

import subprocess

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


您可以将该行放入Process()方法中。

如果您想捕获输出,也可以在documentation中查看。

关于python - 从另一个python文件使用命令行参数调用Python文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59059613/

10-14 17:46
查看更多