我对应该如何正确使用python sh
库(特别是sh.Command()
)感到困惑。基本上,我希望将input_file_a
传递给program_b.py
并将其输出存储在output_file_b
的另一个目录中,我该如何使用python中的sh
库实现此目的?
如果是指输入和输出重定向,则分别参见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()
的完整路径。