how to use subprocess.Popen execute "find" in windows
clyfish at gmail.com
clyfish at gmail.com
Wed May 7 23:11:04 EDT 2008
On 5月7日, 下午2时41分, alito <alito... at gmail.com> wrote:
> On May 6, 7:19 pm, clyf... at gmail.com wrote:
>
> > In cmd, I can use find like this.
>
> > C:\>netstat -an | find "445"
> > TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
> > UDP 0.0.0.0:445 *:*
>
> > C:\>
>
> > And os.system is OK.>>> import os
> > >>> os.system('netstat -an | find "445"')
>
> > TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
> > UDP 0.0.0.0:445 *:*
> > 0
>
> > But I don't know how to use subprocess.Popen to do this.
>
> > from subprocess import Popen, PIPE
>
> > p1 = Popen(['netstat', '-an'], stdout = PIPE)
> > p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
> > print p2.stdout.read()
>
> Get rid of the extra quotes. ie:
> p2 = Popen(['find', '445'], stdin = p1.stdout, stdout = PIPE)
>
> The quotes on the command line and on the os.system call are consumed
> by the shell. The program doesn't see them.
You must be a linux user:)
I guess, in windows, the quotes are consumed by the c runtime library.
Mayby the "find" in windows doesn't use the argc/argv but the windows
API GetCommandLine().
I wrote a c program to prove it.
#include <windows.h>
#include <stdio.h>
int main(int argc, char **argv) {
int i;
printf("%s\n", GetCommandLine());
for (i = 0; i < argc; ++i)
printf("%d: %s\n", i, argv[i]);
return 0;
}
The output is:
C:\>test 1 2 "3"
test 1 2 "3"
0: test
1: 1
2: 2
3: 3
C:\>
Notice that, GetCommandLine() does not consume the quotes, but the
(char **argv) does.
More information about the Python-list
mailing list