简体   繁体   English

Python 运行与我的脚本不同的脚本

[英]Python run separate scripts from my script

I have 2 scripts, and I need to run one script from another, get its output, and assign it into variable inside the first script, and use it.我有 2 个脚本,我需要从另一个脚本运行一个脚本,获取其输出,并将其分配给第一个脚本内的变量,然后使用它。

The second script need to give me some output of a text within 5-7 characters.第二个脚本需要给我一些 5-7 个字符以内的文本输出。

I tried to run this in my first script, but it didn't work.我试图在我的第一个脚本中运行它,但它没有用。

import subprocess

capnum = subprocess.run(["../python3 test-ver2.py"])
print("Result: " % capnum.returncode)

I believe subprocess.getoutput() is what you're looking for:我相信subprocess.getoutput()是你正在寻找的:

import subprocess

capnum = subprocess.getoutput("..\\python3 test-ver2.py")
print(f"Result: {capnum}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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