如何组合两个python脚本

2024-10-02 00:23:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我的一个python脚本给出了几个输出(例如1067879400106788060010678812001067881800106788240010678830001067883600)。现在我想将这些数字或参数提供给另一个python脚本。所以,如何在一个脚本中包含这两个脚本,以便将第一个脚本的输出作为命令行参数提供给另一个脚本呢。在

谢谢

-病毒性


Tags: 命令行脚本参数数字病毒性
2条回答

请考虑以下选项:

1)使用如下管道从shell运行两个脚本:

first.py | second.py

2)重新编写脚本,以便在第二个脚本中导入第一个脚本,例如:

第一个脚本:

^{pr2}$

第二个脚本:

# second.py
from .first import get_numbers

# the method using the numbers (formerly getting them from stdin)
def process_numbers():
    numbers = get_numbers()
    # do something to process the numbers

如果您确实想按原样调用另一个脚本,可以这样做:

#second.py
from subprocess import Popen, PIPE

def process_numbers():
    p = Popen(["first.py", "argument"], stdout=PIPE, stderr=PIPE)
    out, err = p.communicate()
    # the out variable now contains the standard output of the first script
    # do something to process the numbers

如果你真的要这样做,你可以考虑一下:

os.system("script2.py arg1 arg2")

对于list或dict,在准备要传递/接收的数据时,可以考虑使用pickle。在

相关问题 更多 >

    热门问题