Python 3.x 如何在Python中运行命令行脚本

Python 3.x 如何在Python中运行命令行脚本,python-3.x,Python 3.x,如果我有这样的命令行: abc -c config.json| xyz -c test.json 如何在Python文件中运行它?我的意思是我们不会在终端中键入“abc-c config.json | xyz-c test.json”。 xyz和abc是我编写的应用程序 那么,我可以帮忙吗 你的问题让我有点困惑 如果您想从python内部进行系统调用,那么可以使用subprocess模块 x = subprocess.Popen("ls /home", shell=True, stdout=s

如果我有这样的命令行:

abc -c config.json| xyz -c test.json
如何在Python文件中运行它?我的意思是我们不会在终端中键入“
abc-c config.json | xyz-c test.json
”。
xyz
abc
是我编写的应用程序


那么,我可以帮忙吗

你的问题让我有点困惑

如果您想从python内部进行系统调用,那么可以使用subprocess模块

x = subprocess.Popen("ls /home", shell=True, stdout=subprocess.PIPE).stdout.read() x=subprocess.Popen(“ls/home”,shell=True,stdout=subprocess.PIPE).stdout.read() 如果要从命令行运行python命令,可以将其写入文件并执行

python myFile.py python myFile.py 或者,直接向python运行这些命令

python -c "print("testing")"
python-c“打印(“测试”)”你的问题让我有点困惑

如果您想从python内部进行系统调用,那么可以使用subprocess模块

x = subprocess.Popen("ls /home", shell=True, stdout=subprocess.PIPE).stdout.read() x=subprocess.Popen(“ls/home”,shell=True,stdout=subprocess.PIPE).stdout.read() 如果要从命令行运行python命令,可以将其写入文件并执行

python myFile.py python myFile.py 或者,直接向python运行这些命令

python -c "print("testing")" python-c“打印(“测试”)”您可以使用

os.system(“abc-c config.json | xyz-c test.json”)

这就像在命令提示符下运行
abc-c config.json | xyz-c test.json

您可以使用它

os.system(“abc-c config.json | xyz-c test.json”)


这就像在命令提示符下运行
abc-c config.json | xyz-c test.json

对不起,我刚刚更新了我的问题。是的,我知道子流程,但是我如何使用如下命令行运行:abc-c config.json | xyz-c test.json。我尝试过:subprocess.Popen(shlex.split(“abc-c config.json | xyz-c test.json”)、stdin=subprocess.PIPE、stdout=subprocess.PIPE),但它对我不起作用。什么是“abc”和“xyz”?这些是您编写的应用程序吗?是的,这些是我编写的应用程序,@samson4649。为什么要使用“shlex.split()?您应该能够运行->subprocess.Popen(“abc-c config.json | xyz-c test.json”),stdin=subprocess.PIPE,stdout=subprocess.PIPE)对不起,我刚刚更新了我的问题。是的,我知道子流程,但是我如何使用如下命令行运行:abc-c config.json | xyz-c test.json。我尝试过:subprocess.Popen(shlex.split(“abc-c config.json | xyz-c test.json”)、stdin=subprocess.PIPE、stdout=subprocess.PIPE),但它对我不起作用。什么是“abc”和“xyz”?这些是您编写的应用程序吗?是的,这些是我编写的应用程序,@samson4649。为什么要使用“shlex.split()?您应该能够运行->subprocess.Popen(“abc-c config.json | xyz-c test.json”),stdin=subprocess.PIPE,stdout=subprocess.PIPE)