Python Subprocess module commandline program

I'm trying to start a .exe program that is based on command line. The process starts but after 2 seconds, it just vanishes from the task manager. But, when I try to start a .exe that is a GUI, for example firefox.exe, then the application normally opens. Support Info:After experimenting, opening the cmd.exe with Popen with stdin=subprocess.PIPE and stdout=subprocess.PIPE, I can communicat

Python子进程模块命令行程序

我试图启动一个基于命令行的.exe程序。 该过程开始,但2秒后,它只是从任务管理器中消失。 但是,当我尝试启动一个作为GUI的.exe文件时,例如firefox.exe,那么应用程序通常会打开。 支持信息:在试验后,用Pidn打开带有stdin = subprocess.PIPE和stdout = subprocess.PIPE的cmd.exe,我可以实时与cmd通信。但是通过strdin执行.exe(写入process.stdin.write( “something.exe n”),行为与前一段落相同。像子进程没有给cmd

python popen pipe in loop

I'm trying to write a function to create a shell pipeline in a loop that gets its command parameters from a list and pipes the last stdout to the new stdin. At the and of the command list, I want to call the communicate method on the Popen object to get the output. The output is always empty. What am I doing wrong? See following example: lstCmd = ["tasklist", "grep %SESSIONNAME%", "gre

循环中的python popen管道

我试图编写一个函数在循环中创建一个shell管道,从一个列表中获取其命令参数,并将最后一个stdout传递给新的stdin。 在命令列表中,我想调用Popen对象的通信方法来获取输出。 输出总是空的。 我究竟做错了什么? 看下面的例子: lstCmd = ["tasklist", "grep %SESSIONNAME%", "grep %s" % (strAutName)] lstPopen = [subprocess.Popen(lstCmd[0].split(), stdout=subprocess.PIPE)] for i in range(len(lstCmd) - 1):

Output of subprocess both to PIPE and directly to stdout

I found a number of questions which looks like mine, but which did not produce a solution I can use (closest is: subprocess output to stdout and to PIPE) The problem: I want to start a process using subprocess which takes a long time. After running the command I need to parse the stdout-output and the stderr-output. Currently I do it as follows: p = subprocess.Popen( command_list, stdout=su

子过程输出到PIPE并直接输出到stdout

我发现了一些类似于我的问题,但没有提供我可以使用的解决方案(最接近的是:子过程输出到标准输出和PIPE) 问题:我想使用需要很长时间的子进程启动一个进程。 运行命令后,我需要解析stdout-output和stderr-output。 目前我做如下: p = subprocess.Popen( command_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) out, error_msg = p.communicate() print out + "nn" + error_msg #next comes code in wh

when is 'commands' preferable to 'popen' subprocess?

I'm apprenticing into system administration without schooling, so sometimes I'm missing what is elementary information to many others. I'm attempting to give my stdout line another argument before printing, but I'm not sure which process I should use, and I'm a bit fuzzy on the commands for subprocess if that's what I should be using. My current code is: f = open('fil

什么时候'命令'更喜欢'popen'子流程?

我在没有学校教育的情况下进入系统管理学习,所以有时我会错过许多其他基本信息。 我试图在打印之前给我的stdout行提供另一个参数,但是我不确定我应该使用哪个过程,并且我对子进程的命令有点模糊,如果这就是我应该使用的。 我目前的代码是: f = open('filelist', 'r') searchterm = f.readline() f.close()| #takes line from a separate file and gives it definition so that it may be callable. import commands co

Need to avoid subprocess deadlock without communicate

I need a execute a command that produces a lot of output and takes a lot of time to execute (> 30 minutes). I was thinking of using subprocess.Popen to do it. I need to capture the output of the command, so I pass PIPE to stdout and stderr. A deadlock problem when using Popen.wait() is well documented on a lot of forums, so Popen.communicate() is the proposed way of avoiding the deadlock.

没有通信需要避免子进程死锁

我需要一个执行一个命令来产生大量的输出,并且需要很多时间来执行(> 30分钟)。 我正在考虑使用subprocess.Popen来做到这一点。 我需要捕获命令的输出,所以我将PIPE传递给stdout和stderr。 使用Popen.wait()时遇到的死锁问题在很多论坛中都有详细记录,所以Popen.communicate()是避免死锁的建议方式。 该解决方案的问题是通信()阻塞,直到命令完成。 我需要打印执行命令时到达标准输出的所有内容。 如果20分

newbie python subprocess: "write error: Broken pipe"

Thanks to the helpful suggestions below: So it seems to be fixed when I separate commands into individual calls to Popen stderr=subprocess.PIPE as an argument to each Popen chain. The New code: import subprocess import shlex import logging def run_shell_commands(cmds): """ Run commands and return output from last call to subprocess.Popen. For usage see the test below. ""

新手python子进程:“写入错误:断开的管道”

感谢以下有用的建议: 所以它似乎是固定的,当我 将命令分成对Popen的个别呼叫 stderr = subprocess.PIPE作为每个Popen链的参数。 新代码: import subprocess import shlex import logging def run_shell_commands(cmds): """ Run commands and return output from last call to subprocess.Popen. For usage see the test below. """ # split the commands cmds = cmds.split("|") cmds

How do I use subprocess.Popen to connect multiple processes by pipes?

How do I execute the following shell command using the Python subprocess module? echo "input data" | awk -f script.awk | sort > outfile.txt The input data will come from a string, so I don't actually need echo . I've got this far, can anyone explain how I get it to pipe through sort too? p_awk = subprocess.Popen(["awk","-f","script.awk"], stdin=subprocess.

如何使用subprocess.Popen通过管道连接多个进程?

如何使用Python subprocess模块执行以下shell命令? echo "input data" | awk -f script.awk | sort > outfile.txt 输入数据将来自一个字符串,所以我实际上并不需要echo 。 我有这么远,任何人都可以解释我如何通过sort这个问题吗? p_awk = subprocess.Popen(["awk","-f","script.awk"], stdin=subprocess.PIPE, stdout=file("outfile.txt", "w")) p_awk.communica

Escaping regex string in Python

I want to use input from a user as a regex pattern for a search over some text. It works, but how I can handle cases where user puts characters that have meaning in regex? For example, the user wants to search for Word (s) : regex engine will take the (s) as a group. I want it to treat it like a string "(s)" . I can run replace on user input and replace the ( with ( and the ) with )

在Python中转义正则表达式字符串

我想使用用户输入作为正则表达式来搜索某些文本。 它有效,但我如何处理用户在正则表达式中放置有意义的字符的情况? 例如,用户想要搜索Word (s) :正则表达式引擎将把(s)作为一个组。 我希望它把它当作一个字符串"(s)"来对待。 我可以运行replace用户输入并替换(用(和)与)但问题是我需要做替换每个可能的正则表达式符号。 你知道更好的方法吗? 使用re.escape()函数来实现这一点: 4.2.3 re模块内容 逃生

Regex to match string that does not have a preceding string

I'm using python to parse some files with multiple lines and perform regex using re.findall() . I'm trying to match lines with the string "bar" as long as the string "no foo " is NOT in front of "bar". In other words - match any "bar" but do not match "no foo bar". If "bar" appears outside of the "no foo bar" then i

正则表达式来匹配没有前面字符串的字符串

我使用python来分析多行文件,并使用re.findall()执行regex。 只要字符串“no foo”不在“bar”之前,我正尝试将字符串与“bar”匹配。 换句话说 - 匹配任何“酒吧”,但不匹配“没有富酒吧”。 如果“bar”出现在“no foo bar”之外,那么它应该匹配。 输入: 1. candy bar 1 2. no bar stool 3. no foo bar here 4. foo barred 5. still no foo bar. 6. foo bar! 7. foobar! 8. tricky no foo bar but has bar again 期望的输出: 1.

Python regex to match a specific word

I want to match all lines in a test report, which contain words 'Not Ok'. Example line of text : 'Test result 1: Not Ok -31.08' I tried this: filter1 = re.compile("Not Ok") for line in myfile: if filter1.match(line): print line which should work according to http://rubular.com/, but I get nothing at the output. Any idea, what might be

Python正则表达式匹配一个特定的单词

我想匹配测试报告中的所有行,其中包含单词'Not Ok'。 示例文本行: 'Test result 1: Not Ok -31.08' 我试过这个: filter1 = re.compile("Not Ok") for line in myfile: if filter1.match(line): print line 它应该按照http://rubular.com/来工作,但我在输出中什么都没有得到。 任何想法,可能是错误的? 测试了各种其他参数,如“。” 和“^测试”,完美的工作