By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,017 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,017 IT Pros & Developers. It's quick & easy.

how to use subprocess.Popen execute "find" in windows

P: n/a
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()

It doesn't work.
Because subprocess.Popen execute "find" like this.

C:\>find \"445\"
拒绝访问 - \

C:\>

It adds a '\' before each '"'.
How to remove the '\'?
Thank you.
Jun 27 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
cl*****@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.
While there certainly are valid usecases for piping with subprocess in
Python - this ain't one I'd say. Just read the output of netstat yourself,
and filter for lines that contain the desired pattern.

Diez
Jun 27 '08 #2

P: n/a
On May 6, 11:19 am, clyf...@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()
I would say that, according to documentation, the following should
work:
print p2.communicate()[0]
Philippe
Jun 27 '08 #3

P: n/a
On May 6, 5:19 pm, clyf...@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()

It doesn't work.
Because subprocess.Popen execute "find" like this.

C:\>find \"445\"
拒绝访问 - \

C:\>

It adds a '\' before each '"'.
How to remove the '\'?
Thank you.
cannot help with the backslashes but try findstr instead of find
Jun 27 '08 #4

P: n/a
On May 6, 7:19*pm, clyf...@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.
Jun 27 '08 #5

P: n/a
On 5月7日, 上午9时45分, Justin Ezequiel <justin.mailingli...@gmail.com>
wrote:
On May 6, 5:19 pm, clyf...@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()
It doesn't work.
Because subprocess.Popen execute "find" like this.
C:\>find \"445\"
拒绝访问 - \
C:\>
It adds a '\' before each '"'.
How to remove the '\'?
Thank you.

cannot help with the backslashes but try findstr instead of find
Thank you.
findstr doesn't need quotes, so it works.
Jun 27 '08 #6

P: n/a
On 5月7日, 下午2时41分, alito <alito...@gmail.comwrote:
On May 6, 7:19 pm, clyf...@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.
Jun 27 '08 #7

P: n/a
En Wed, 07 May 2008 23:29:58 -0300, <cl*****@gmail.comescribi贸:
On 5鏈7鏃, 涓婂崍9鏃45鍒, Justin Ezequiel <justin.mailingli...@gmail.com>
wrote:
>On May 6, 5:19 pm, clyf...@gmail.com wrote:
p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read()
It doesn't work.
Because subprocess.Popen execute "find" like this.
C:\>find \"445\"
It adds a '\' before each '"'.
How to remove the '\'?
Thank you.

cannot help with the backslashes but try findstr instead of find

Thank you.
findstr doesn't need quotes, so it works.
Build the command line yourself -instead of using a list of arguments-. Popen doesn't play with the quotes in that case:

p1 = Popen(['netstat', '-an'], stdout = PIPE) # using list
p2 = Popen('find "445"', stdin = p1.stdout, stdout = PIPE) # using str
print p2.communicate()[0]

--
Gabriel Genellina

Jun 27 '08 #8

P: n/a
On 5月8日, 下午5时39分, "Gabriel Genellina" <gagsl-....@yahoo.com.arwrote:
En Wed, 07 May 2008 23:29:58 -0300, <clyf...@gmail.comescribió:
On 5月7日, 上午9时45分, Justin Ezequiel <justin.mailingli...@gmail.com>
wrote:
On May 6, 5:19 pm, clyf...@gmail.com wrote:
p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read()
It doesn't work.
Because subprocess.Popen execute "find" like this.
C:\>find \"445\"
It adds a '\' before each '"'.
How to remove the '\'?
Thank you.
cannot help with the backslashes but try findstr instead of find
Thank you.
findstr doesn't need quotes, so it works.

Build the command line yourself -instead of using a list of arguments-. Popen doesn't play with the quotes in that case:

p1 = Popen(['netstat', '-an'], stdout = PIPE) # using list
p2 = Popen('find "445"', stdin = p1.stdout, stdout = PIPE) # using str
print p2.communicate()[0]

--
Gabriel Genellina
Thanks very much.
You solved my problem.
Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.