How do I write to a Python subprocess' stdin?

后端 未结 3 1375
鱼传尺愫
鱼传尺愫 2020-11-22 15:50

I\'m trying to write a Python script that starts a subprocess, and writes to the subprocess stdin. I\'d also like to be able to determine an action to be taken if the subpro

3条回答
  •  忘掉有多难
    2020-11-22 15:50

    You can provide a file-like object to the stdin argument of subprocess.call().

    The documentation for the Popen object applies here.

    To capture the output, you should instead use subprocess.check_output(), which takes similar arguments. From the documentation:

    >>> subprocess.check_output(
    ...     "ls non_existent_file; exit 0",
    ...     stderr=subprocess.STDOUT,
    ...     shell=True)
    'ls: non_existent_file: No such file or directory\n'
    

提交回复
热议问题