Python 子进程不采用正确的参数 [英] Python subprocess does not take correct arguments

查看:32
本文介绍了Python 子进程不采用正确的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Windows 和 32 位 Python 2.7.

I am using Windows and 32-bit Python 2.7.

我已经阅读了许多涉及让 Python 中的子进程模块正常工作的帖子 - 使 shell = True、单个字符串与字符串列表、使用原始字符串等.我感到困惑的是 Python 不仅失败生成我正在执行的程序的输出,但也无法运行文档中介绍的一些命令.

I have already read many posts that involves getting the subprocess module in Python work properly - making shell = True, single string vs. list of strings, using raw strings etc. What I am confused about is that the Python not only fails to produce an output of a program I am executing, but also fails to run some of the commands introduced in the documentation.

例如,当我尝试使用 https://docs.python.org/2/library/subprocess.html#subprocess.check_call 在 python 交互式控制台中,它产生WindowsError: [错误 2] 系统找不到指定的文件".

For instance, when I try to use "subprocess.check_call(["ls", "-l"])" as introduced in https://docs.python.org/2/library/subprocess.html#subprocess.check_call in python interactive console, it produce "WindowsError: [Error 2] The system cannot find the file specified".

同样,当我尝试使用subprocess.call(["ls", "-l"])"时,它完全出现在文档中,Python 再次产生WindowsError: [Error 2] The system cannot find指定的文件"错误.如果我使用subprocess.call(["ls", "-l"], shell = True)",它只会正确执行并返回退出状态 1,这与我在文档页面上读到的不同.

Similarly, when I try to use "subprocess.call(["ls", "-l"])" as it appears exactly in the documentation, Python once again produces "WindowsError: [Error 2] The system cannot find the file specified" error. It only executes correctly and returns the exit status 1 if I use "subprocess.call(["ls", "-l"], shell = True)", different from what I read on the doc page.

更重要的是,我希望通过 Python 执行一个 Windows 可执行程序.我已经确认程序在 Cygwin 终端中正常运行,使用 Python 执行时它不打印任何输出(我注意到这个问题已经被问过几次,但解决方案对我不起作用).

More to the point, there is a Windows executable program which I wish to execute via Python. I have confirmed the program functions properly in the Cygwin terminal, it does not print any output when executed with Python (I noticed that this problem has been asked a few times, but the solutions did not work for me).

import subprocess as sub

rt = sub.Popen([r'C:/Users/Y L/Documents/ssocr', '-d', '-1', 'Sample.JPG'], stdin = sub.PIPE, stdout = sub.PIPE, stderr = sub.PIPE)
out, err = rt.communicate()
print(out, err)

当我打印 (out, err) 时,这会生成一个空字符串对的元组.更有趣的是,当传入的图像文件完全是乱码时,程序以相同的方式执行并产生相同的输出,这意味着参数甚至没有正确传入.

When I print (out, err), this generates a tuple of an empty string pair. More interestingly, the program executes the same way and produces an identical output when the image file passed in is a total gibberish, which implies the arguments are not even being passed in properly.

import subprocess as sub

rt = sub.Popen([r'C:/Users/Y L/Documents/ssocr', '-d', '-1', 'asdfasdf.JPG'], stdin = sub.PIPE, stdout = sub.PIPE, stderr = sub.PIPE)
out, err = rt.communicate()
print(out, err)

关于子进程模块处理的参数,我是否遗漏了什么?

Is there something I am missing about the arguments are handled by the subprocess module?

推荐答案

ls 是类 Unix 系统的命令,在 Windows 下不存在.几乎等效的命令是 cmd/c dir 因为 dircmd 的内部命令.

ls is a command of Unix like systems and does not exist under Windows. An almost equivalent command would be cmd /c dir because dir is an internal command of cmd.

在 Windows 下,您可以先在 cmd 窗口下直接执行命令,然后将单个命令行传递给 Popen(并添加 cmd/c 如果命令是 cmd 内部命令)

Under Windows, you could have better luck with first executing the command directly under a cmd windows, and then passing a single command line to Popen (and add cmd /c first if the command is a cmd internal command)

这篇关于Python 子进程不采用正确的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆