472,989 Members | 3,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

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

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
8 4036
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Galsaba | last post by:
I am trying to find a script to a "dealer locator" or "find a store near you". I will have two tables. One will include all US and Canada ZIPs including lat and longtitudes. The other one is a...
3
by: Dmitry Jouravlev | last post by:
Hi, I have a number of C++ solutions in Visual Studio .NET and when i compile them using "Whole Program Optimization", certain projects report a LNK1171 error saying that c2.dll could not be...
1
by: Bart Masschelein | last post by:
Hello, I have a VC++ project which uses files located on my Unix account Browsing (eg. go to Definition etc) works fine, however, if I use "Fin in Files" it does not find any single instance of...
2
by: Lenonardo | last post by:
Hi. I'm writing a VB.Net application to update multiple Excel Worksheets. I'm using late binding (i.e. all variables are objects + use createobject) I develop the application on an XP...
2
by: Bill nguyen | last post by:
How can I get a listbox to display the most closely matched items when user types in the first few letters/digits of displaymember value? Thanks a million Bill
0
by: Rave | last post by:
This is a long shot, but I thought I'd try it. I am currently using excel as an inventory tool. I currently have a hand-held scanner plugged into a laptop for reading barcodes. Using the "Find and...
0
by: zensunni | last post by:
The script I've made checks for identical fields in the database before it inserts a new row. However, there is one number that, when checked for, will not be found. It assumes there is no row, so it...
1
by: vps | last post by:
I have Apache 2.2 server running on Windows XP SP2 with PHP 5.2.5. I have run go-pear.bat in PHP directory (system-wide, all defaults). Running 損ear command lists additional commands. I...
1
by: sidd4one | last post by:
Hi Hi friends when ever i start my system i use get the one message as windows can not find "CSRCS.EXE".i tried everthing to delete but i can't.But friends i...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.