Python 2.7.3 process.Popen()失败

时间:2021-08-16 22:00:51

I'm using python 2.7.3 on a Windows 7 64-bit machine currently (also developing in Linux 32 bit, Ubuntu 12.04) and am having odd difficulties getting python to communicate with the command prompt/terminal successfully. My code looks like this:

我目前正在Windows 7 64位机器上使用python 2.7.3(也在Linux 32位上开发,Ubuntu 12.04),并且让python成功地与命令提示符/终端通信有一些奇怪的困难。我的代码是这样的:

import subprocess, logging, platform, ctypes

class someClass (object):

def runTerminalCommand:

    try:
        terminalOrCmdLineCommand = self.toString()

        process = subprocess.Popen(terminalOrCmdLineCommand, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        output = process.stdout.readlines()

        if len(output) < 1:
            logging.exception("{} error : {}".format(self.cmd,process.stderr.readlines()))
            raise ConfigError("cmd issue : {}".format(self.cmd))
        return output

    except ValueError as err:
        raise err
    except Exception as e:
        logging.exception("Unexpected error : " + e.message)
        raise ConfigError("unexpected error")

Now, I know that the self.toString() returned value will process correctly if I enter it manually, so I'm limiting this to an issue with how I'm sending it to the command line via the subprocess. I've read the documentation, and found that the subprocess.check_call() doesn't return anything if it encounters an error, so I'm using .Popen()

现在,我知道如果我手动输入,self.toString()返回的值将会正确处理,所以我将这个问题限制为如何通过子进程将它发送到命令行。我阅读了文档,发现如果遇到错误,subprocess.check_call()不会返回任何内容,所以我使用。popen ()

The exception I get is,

我得到的例外是,

    [date & time] ERROR: Unexpected error :
    Traceback (most recent call last):
      File "C:\[...]"
        raise ConfigError("cmd issue : {}".format(self.cmd))
    "ConfigError: cmd issue : [the list self.cmd printed out]"

What I am TRYING to do, is run a command, and read the input back. But I seem to be unable to automate the call I want to run. :(

我要做的是,运行一个命令,然后读取输入。但我似乎无法自动运行我想运行的电话。:(

Any thoughts? (please let me know if there are any needed details I left out)

任何想法吗?(如有需要我遗漏的资料,请告知)

Much appreciated, in advance.

提前感谢,。

1 个解决方案

#1


10  

The docs say:

医生说:

Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.

使用communication()而不是。stdin。写,.stdout。读或.stderr。请阅读以避免死锁,因为其他操作系统的管道缓冲区被填满并阻塞了子进程。

You could use .communicate() as follows:

您可以使用。communication()如下:

p = Popen(cmd, stdout=PIPE, stderr=PIPE)
stdout_data, stderr_data = p.communicate()
if p.returncode != 0:
    raise RuntimeError("%r failed, status code %s stdout %r stderr %r" % (
                       cmd, p.returncode, stdout_data, stderr_data))
output_lines = stdout_data.splitlines() # you could also use `keepends=True`

See other methods to get subprocess output in Python.

请参阅在Python中获取子进程输出的其他方法。

#1


10  

The docs say:

医生说:

Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.

使用communication()而不是。stdin。写,.stdout。读或.stderr。请阅读以避免死锁,因为其他操作系统的管道缓冲区被填满并阻塞了子进程。

You could use .communicate() as follows:

您可以使用。communication()如下:

p = Popen(cmd, stdout=PIPE, stderr=PIPE)
stdout_data, stderr_data = p.communicate()
if p.returncode != 0:
    raise RuntimeError("%r failed, status code %s stdout %r stderr %r" % (
                       cmd, p.returncode, stdout_data, stderr_data))
output_lines = stdout_data.splitlines() # you could also use `keepends=True`

See other methods to get subprocess output in Python.

请参阅在Python中获取子进程输出的其他方法。