简体   繁体   English

如何停止subprocess.Popen在python中等待用户输入

[英]How to stop subprocess.Popen waiting for user input in python

I have the following code in python: 我在python中有以下代码:

import subprocess
var = subprocess.Popen(["pdflatex","file.tex"])

Where var is a throw away variable because I don't care about the return variable. var是一个丢弃变量,因为我不在乎返回变量。 It does what I want (generates a pdf file from the latex file file.tex), but then waits for user input. 它完成了我想要的操作(从乳胶文件file.tex生成pdf文件),但是随后等待用户输入。 These are the last lines of the program and I just want it to end without waiting for input. 这些是程序的最后几行,我只希望它结束​​而不必等待输入。 How can I do this? 我怎样才能做到这一点?

Redirect stdin to devnull, to avoid a subprocess to pause waiting for user input: 将stdin重定向到devnull,以避免子进程暂停等待用户输入:

import os
from subprocess import Popen
try:
    from subprocess import DEVNULL # Python 3
except ImportError:
    DEVNULL = open(os.devnull, 'r+b', 0)

Popen(['pdflatex', 'file.tex'], stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL)

I assume that you don't care about stdout/stderr also. 我假设您也不关心stdout / stderr。

If you're not concerned about the output, try this: 如果您不关心输出,请尝试以下操作:

args = ["pdflatex","file.tex"]
var = subprocess.Popen(args, stdout=subprocess.PIPE)
trash = var.communicate() # dump the output of the program calls

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

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