将文件作为输入传递给程序,并使用python中的sh库存储其输出

问题描述 投票:0回答:1

我对应该如何正确使用python sh库(特别是sh.Command())感到困惑。基本上,我希望将input_file_a传递给program_b.py并将其输出存储在output_file_b的另一个目录中,我该如何使用python中的sh库实现此目的?

python mapreduce sh
1个回答
0
投票

如果是指输入和输出重定向,则分别参见here (in)here (out)。特别是,看起来要“重定向” stdin,您需要将实际字节作为参数传递(例如,事先读取它们),尤其是以下内容应根据其文档工作(未经测试,因为我没有使用sh -请让我们知道这是否对您有用/修复所有缺失的内容):

import sh
python3 = sh.Command("python3")
with open(input_file_a, 'r') as ifile:
    python3("program_b.py", _in=ifile.read(), _out=output_file_b)

注意,可能需要为sh.Command指定参数search_paths才能找到python。另外,可能需要相应地指定program_b.py文件或os.chdir()的完整路径。

© www.soinside.com 2019 - 2024. All rights reserved.