问题标签 [popen]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
929 浏览

python - 运行外部子进程并读取返回码

我正在创建一个 python 脚本来对大量图像进行排序(游戏截图)。

我在 imagemagick 中找到了一种方法:我知道,如果图像的特定正方形与参考裁剪相同,则图像属于第一类。如果不是,我检查另一个裁剪和另一个类别,如果不适合,我把图像放在第三类。

我在 Imagemagick 中找到了如何做到这一点:

(我剪了一张图片,然后将那张图片与“reference.jpg”进行比较)

我如何从脚本中调用它,并根据 convert 返回的内容(它是一个数字)执行 if ?

0 投票
1 回答
2026 浏览

php - PHP popen 和 proc_open 不返回二进制输出

使用 popen 或 proc_open 运行以下命令以列出 windows 共享的内容时:

如果共享中的文件在遇到特殊字符时具有 § 字符(ASCII 21 十进制),则会跳过数据直到下一行。

预期输出:

实际输出:

0 投票
8 回答
257343 浏览

python - Python subprocess/Popen with a modified environment

I believe that running an external command with a slightly modified environment is a very common case. That's how I tend to do it:

I've got a gut feeling that there's a better way; does it look alright?

0 投票
1 回答
2542 浏览

python - subprocess.Popen(..).communicate(..) 与graphviz一起使用时随机丢弃数据!

我正在使用 graphviz 的 dot 为 Web 应用程序生成一些 svg 图。我使用 Popen 调用 dot:

发生的事情是 dot 程序抛出如下错误:

这个明显的错误肯定不在输入字符串中。特别是,如果我使用 utf-8 编码将其保存到 str.txt 并执行

我得到了想要的输出。str 唯一的“特殊”之处在于它包含像丹麦语 øæå 这样的字符。

现在我不知道我应该做什么。问题很可能出在点上;但它肯定似乎是由 Popen 触发的,它与从 shell 中使用 < 不同,我不知道从哪里开始。任何有关替代调用 dot 的帮助或想法(除了将所有数据写入文件并调用它!)将不胜感激!

0 投票
1 回答
4817 浏览

python - 打印出 os.popen() 的输出而不在 python 中缓冲

假设我有一个打印出一些数据的过程,比如这个 ruby​​ 代码。

我想要一个产生这个过程的python代码,并从中读取数据以打印出来。

这段代码的问题是它没有一个一个地打印数字。似乎python在最后一点打印出所有缓冲数据。

有没有办法在不缓冲的情况下打印出生成进程的输出?

0 投票
5 回答
100925 浏览

python - 将 os.popen(command) 读入字符串

我不确定我的标题是否正确。我正在做的是编写一个 python 脚本来自动化我的一些代码编写。所以我正在解析一个 .h 文件。但我想在开始之前展开所有宏。所以我想调用shell:

哪个应该将 myHeader.h 的后预处理版本输出到标准输出。现在我想将所有输出直接读入一个字符串以进行进一步处理。我读过我可以用 popen 做到这一点,但我从未使用过管道对象。

我该怎么做呢?

0 投票
2 回答
4368 浏览

python - Why does supplying stdin to subprocess.Popen cause what is written to stdout to change?

I'm using Python's subprocess.Popen to perform some FTP using the binary client of the host operating system. I can't use ftplib or any other library for various reasons.

The behavior of the binary seems to change if I attach a stdin handler to the Popen instance. For example, using XP's ftp client, which accepts a text file of commands to issue:

commands.txt:

When also supplying stdin, all you get in stdout is:

Initially I thought this was a quirk of the XP ftp client, perhaps knowing it wasn't in interactive mode and therefore limiting its output. However, the same behaviour happens with OS X's ftp - all the server responses are missing from stdout if stdin is supplied - which leads me to think that this is normal behaviour.

In Windows I can use the -s switch to effectively script ftp without using stdin, but on other platforms one relies on the shell for that kind of interaction.

Python version is 2.6.x on both platforms. Why would supplying a handle for stdin change stdout, and where have the server responses gone to?

0 投票
3 回答
1648 浏览

ruby - Discussion with a sub-process, using Ruby with IO and threading

I am trying to use IO.popen in order to put (with .puts method) and to get (with .gets method) messages from a process to its sub-process.

I am not very experimented and I have a question about. Having the following code, I have an error because it is not possible to write in a closed stream.

(...) in `write': not opened for writing (IOError)

As we can see, this error occur during is_cool? test (as explained at: http://ruby-doc.org/core/classes/IO.html#M002289).

But if I try to comment in process_command method the line:

the script seems to sleep... infinitely :s

I believe that it is not possible to open again a closed stream. And I can't create an other IO.popen instance of my program "./program" because it needs to be initialized with some command (like 'are u ready?' and 'are u cool?') at the beginning, before I use it (by sending and receiving messages like a simple discussion).

How changes can I do over the current code in order to solve this problem?

Edit: in other words, I would like to establish a such communication (according to a given protocol):

Many thanks for any help.

0 投票
2 回答
14439 浏览

python - args 参数的 subprocess.Popen 最大长度是多少?

我正在使用子进程模块中的Popen函数来执行命令行工具:

我正在使用的工具需要一个然后处理的文件列表。在某些情况下,此文件列表可能很长。有没有办法找到 args 参数的最大长度?将大量文件传递给该工具时,我收到以下错误:

有没有找到这个最大长度的通用方法?我在 msdn 上找到了以下文章:命令提示符 (Cmd.exe) 命令行字符串限制,但我不想在值中硬编码。我宁愿在运行时获取值以将命令分解为多个调用。

我在 Windows XP 64 上使用 Python 2.6。

编辑:添加代码示例

出现问题是因为paths列表中的每个实际条目通常是一个非常长的文件路径,并且有数千个。

我不介意将命令分解为对process_file.exe. 我正在寻找一种通用方法来获取 args 的最大长度,以便我知道每次运行要发送多少条路径。

0 投票
3 回答
2081 浏览

python - 使用 -File 选项时如何抑制 Powershell 窗口

我这样称呼Powershell:

我是从 python 脚本调用它,但如果通过快捷方式调用,也会出现同样的问题。我认为该-NonInteractive标志会导致 Poweshell 在隐藏窗口中执行,但事实并非如此。从外部应用程序调用 Powershell 时,有没有办法抑制控制台窗口?

基于 Johannes Rössel 建议的解决方案